触发ListViewItem命令

时间:2017-03-22 10:33:59

标签: c# wpf listview triggers

我有以下ItemsControl处理添加到其中的项目的显示:

 <ControlTemplate x:Key="MyItemsControlTemplate">
    <ItemsControl  x:Name="MyItemsControl" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type mynameSpace:MyClass}}, Path=ItemsSource}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel IsItemsHost="True" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                    <WrapPanel.Resources>                                    
                        <HierarchicalDataTemplate DataType="{x:Type local:MyCustomClass}">
                            <Button Command="{Binding}">
                                <TextBlock  Text="{Binding Path=DisplayValue}"/>
                            </Button>
                        </HierarchicalDataTemplate>                                 
                    </WrapPanel.Resources>
                </WrapPanel>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>
</ControlTemplate>

你可以看到Button有一个Command =“{Binding}”来处理click事件并调用命令。

现在我还想使用ListView来显示相同​​的项目并处理click事件。我从这个链接WPF: How to bind a command to the ListBoxItem using MVVM?看到我必须使用Interaction.Triggers,所以我做了以下事情:

<ControlTemplate x:Key="MyListViewControlTemplate">
    <ListView Name="MyListView" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type mynameSpace:MyClass}}, Path=ItemsSource}">

        <ListView.View>
            <GridView >
                <GridViewColumn Header="Details" DisplayMemberBinding="{Binding Path=DisplayValue}"/>
            </GridView>
        </ListView.View>

        <i:Interaction.Triggers>
            <i:EventTrigger EventName="MouseLeftButtonUp">
                <i:InvokeCommandAction Command="{Binding}"></i:InvokeCommandAction>
            </i:EventTrigger>
        </i:Interaction.Triggers>

    </ListView>
</ControlTemplate>

项目在ListView中正确显示,所以我知道文本显示的绑定是正确的,但我发现ListView的单击处理程序不像Button对象那样触发命令。
有什么想法吗?

如何

1 个答案:

答案 0 :(得分:2)

您应该将交互触发器应用于ListView中的元素。您可以使用CellTemplateTextBlock将交互触发器应用于:{/ p>

<ControlTemplate x:Key="MyListViewControlTemplate">
    <ListView Name="MyListView" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type mynameSpace:MyClass}}, Path=ItemsSource}">
        <ListView.View>
            <GridView >
                <GridViewColumn Header="Details">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding DisplayValue}">
                                        <i:Interaction.Triggers>
                                            <i:EventTrigger EventName="MouseLeftButtonUp">
                                                <i:InvokeCommandAction Command="{Binding}"></i:InvokeCommandAction>
                                            </i:EventTrigger>
                                        </i:Interaction.Triggers>
                            </TextBlock>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
</ControlTemplate>