如何将事件处理程序附加到集合项?

时间:2014-08-13 09:59:37

标签: c# wpf mvvm binding catel

大家好我所有的表格都有消耗列表,我希望将点击效果绑定到可扩展列表中的项目。到现在为止还挺好。我已设法正确显示可扩展,但我无法绑定双击。我在MVVM Catel中做我的项目。

我的XAML:

<Grid>
    <ItemsControl ItemsSource="{Binding Source={StaticResource cvsRoutes}}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Expander Header="{Binding Name}" MinHeight="50">
                    <ListBox>
                       <TextBlock Text="Something" >
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="MouseDoubleClick">
                                <cmd:EventToCommand Command="{Binding OpenNewWindow}"/>
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                        </TextBlock>
                        <TextBlock Text="Something" />
                        <TextBlock Text="Something" />
                        <TextBlock Text="Something" />
                        <TextBlock Text="Something" />
                    </ListBox>
                </Expander>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>

并在ModelView类中我有:

 public RouteViewModel(IMessageService messageService,
        IPleaseWaitService pleaseWaitService, IMapService mapService)
    {
       this.mapService = mapService;
       OpenNewWindow = new Command(CreateNewWindow);
    }

    public Command OpenNewWindow { get; private set; }
    //Method To Open the new window
    public void CreateNewWindow()
    {
        NewWindow.ShowNewWindowMap();
    }

1 个答案:

答案 0 :(得分:1)

您可以查看 DoubleClickToCommand 行为:

https://catelproject.atlassian.net/wiki/display/CTL/DoubleClickToCommand

链接包含有关行为的信息以及我在下面复制的如何使用它的示例:

<ListBox x:Name="listBox" ItemsSource="{Binding PersonCollection}" SelectedItem="{Binding SelectedPerson}">
  <ListBox.ItemTemplate>
    <DataTemplate>
      <Grid>
        <i:Interaction.Behaviors>
          <catel:DoubleClickToCommand Command="{Binding ElementName=listBox, Path=DataContext.Edit}" />
        </i:Interaction.Behaviors>

        <StackPanel Orientation="Horizontal">
          <Label Content="{Binding FirstName}" />
          <Label Content="{Binding MiddleName}" />
          <Label Content="{Binding LastName}" />
        </StackPanel>
      </Grid>
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>
相关问题