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

C# 如何将事件处理程序附加到集合项?,c#,wpf,mvvm,binding,catel,C#,Wpf,Mvvm,Binding,Catel,大家好,我的表单中有可消耗列表,我想将点击效果绑定到可扩展列表中的项目。到现在为止,一直都还不错。我已成功正确显示可展开文件,但无法绑定双击。我正在MVVM Catel中做我的项目 我的XAML: <Grid> <ItemsControl ItemsSource="{Binding Source={StaticResource cvsRoutes}}"> <ItemsControl.ItemTemplate> &

大家好,我的表单中有可消耗列表,我想将点击效果绑定到可扩展列表中的项目。到现在为止,一直都还不错。我已成功正确显示可展开文件,但无法绑定双击。我正在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>
您可以查看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>


有什么问题吗?我建议您使用iVisualizerService在MVVM中显示窗口,而不是直接显示窗口。我的代码肯定没有其他问题。。。它没有显示新的windowWell,这个例子很有效。查看示例存储库中的真实演示:github.com/catel/catel.examples
<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>