【问题标题】:Trigger ListViewItem Command触发 ListViewItem 命令
【发布时间】:2017-08-14 09:55:48
【问题描述】:

我有以下 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}" 处理点击事件并调用命令。

现在我还想使用 ListView 来显示相同​​的项目并处理单击事件。我从这个链接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 对象那样触发命令。
有什么想法吗?

如何

【问题讨论】:

    标签: c# wpf listview triggers


    【解决方案1】:

    您应该将交互触发器应用于ListView 中的元素。您可以将 CellTemplateTextBlock 一起使用,并将交互触发器应用于:

    <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>
    

    【讨论】:

    • 谢谢,这解决了我的问题。接下来我想在我的数据网格中有 2 列,所以我使用了你的方法并添加了两个带有 Interaction.Triggers 的内部 TextBlock。有没有办法可以将整个项目包装在一个触发器中?我发现如果我单击文本框右侧的行,则事件不会触发。
    • 很遗憾,您不能在样式中使用交互触发器。另一种方法可能是使用调用命令的附加行为:codeproject.com/Articles/28959/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-13
    • 2019-10-09
    • 2022-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多