在WPF中的Listview分组中检索GroupName

时间:2016-10-14 13:51:29

标签: c# wpf xaml listview mvvm

我已经使用分组设置了ListView,我想在右键单击MVVM中的组时检索GroupName。我在我的组样式上放置了一个ContextMenu,我试图使用System.Windows.Interactivity中的EventToCommand来获取基础项。

这里是相关部分:

<ListView.GroupStyle>
    <GroupStyle>
        <GroupStyle.ContainerStyle>
            <Style TargetType="{x:Type GroupItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Expander IsExpanded="False" Template="{StaticResource CustomizedExpander}" Background="#FFEBEBEB" BorderThickness="0" ContextMenu="{StaticResource GroupContextMenu}">
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="MouseRightButtonDown">
                                        <i:InvokeCommandAction Command="{Binding Path=OnCategorySelected}" CommandParameter="{Binding Name}"/>
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>

                                <Expander.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <!--N.B. The "Name" property is part of the CollectionViewSource-->
                                        <TextBlock Text="{Binding Name}" FontWeight="Bold" Foreground="#FF767676" VerticalAlignment="Bottom" />
                                        <TextBlock Text="{Binding ItemCount}" Foreground="#FF454545" FontWeight="Bold" FontStyle="Italic" Margin="10,0,0,0" VerticalAlignment="Bottom" />
                                        <TextBlock Text=" item(s)" Foreground="#FF767676" FontStyle="Italic" VerticalAlignment="Bottom" />
                                    </StackPanel>
                                </Expander.Header>
                                <ItemsPresenter />
                            </Expander>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </GroupStyle.ContainerStyle>
    </GroupStyle>
</ListView.GroupStyle>

我不知道这是否是正确的方法,但似乎根本没有触发命令。

有什么建议吗?

更新:

整件事情比我想象的要简单得多。根本不需要交互部分。修复绑定足以在显示上下文菜单时获取基础类别:

<ListView ItemsSource="{Binding Modifications}" SelectedItem="{Binding SelectedItem}">
    <ListView.Resources>
        <ContextMenu x:Key="ItemContextMenu">
            <MenuItem Header="Execute" Command="{Binding Path=DataContext.OnExecuteScript, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListView}}" Background="WhiteSmoke" Visibility="{Binding CanExecute, Converter={StaticResource BooleanToVisibilityConverter}}" />
            <MenuItem Header="Execute up to this" Command="{Binding Path=DataContext.OnExecuteScriptUpToThis, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListView}}" Background="WhiteSmoke" Visibility="{Binding CanOnlyBeExecutedSequentially, Converter={StaticResource BooleanToVisibilityConverter}}" />
            <MenuItem Header="Drop" Command="{Binding Path=DataContext.OnExecuteDrop, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListView}}" Visibility="{Binding Drop, Converter={StaticResource BooleanToVisibilityConverter}}" Background="WhiteSmoke" />
            <MenuItem Header="Dump" Command="{Binding Path=DataContext.OnExecuteDump, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListView}}" Visibility="{Binding CanDump, Converter={StaticResource BooleanToVisibilityConverter}}" Background="WhiteSmoke" />
        </ContextMenu>
        <ContextMenu x:Key="GroupContextMenu">
            <MenuItem Header="Dump all" Command="{Binding Path=DataContext.OnExecuteDumpAll, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListView}}" CommandParameter="{Binding Name}" Background="WhiteSmoke" />
        </ContextMenu>
    </ListView.Resources>
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Type" Width="120" DisplayMemberBinding="{Binding Type}" />
            <GridViewColumn Header="Object Name" Width="Auto" DisplayMemberBinding="{Binding DisplayName}" />
            <GridViewColumn Header="" Width="50">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox IsChecked="{Binding Deploy}" IsHitTestVisible="False" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Header="Object Name" Width="300" DisplayMemberBinding="{Binding ObjectName}" />
        </GridView>
    </ListView.View>
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}" BasedOn="{StaticResource MetroListViewItem}" >
            <Setter Property="ContextMenu" Value="{StaticResource ItemContextMenu}" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding Drop}" Value="True">
                    <Setter Property="Foreground" Value="Red" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ListView.ItemContainerStyle>
    <ListView.GroupStyle>
        <GroupStyle>
            <GroupStyle.ContainerStyle>
                <Style TargetType="{x:Type GroupItem}">
                    <Setter Property="ContextMenu" Value="{StaticResource GroupContextMenu}" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <Expander IsExpanded="False" Template="{StaticResource CustomizedExpander}" Background="#FFEBEBEB" BorderThickness="0">
                                    <Expander.Header>
                                        <StackPanel Orientation="Horizontal">
                                            <!--N.B. The "Name" property is part of the CollectionViewSource-->
                                            <TextBlock Text="{Binding Name}" FontWeight="Bold" Foreground="#FF767676" VerticalAlignment="Bottom" />
                                            <TextBlock Text="{Binding ItemCount}" Foreground="#FF454545" FontWeight="Bold" FontStyle="Italic" Margin="10,0,0,0" VerticalAlignment="Bottom" />
                                            <TextBlock Text=" item(s)" Foreground="#FF767676" FontStyle="Italic" VerticalAlignment="Bottom" />
                                        </StackPanel>
                                    </Expander.Header>
                                    <ItemsPresenter />
                                </Expander>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </GroupStyle.ContainerStyle>
        </GroupStyle>
    </ListView.GroupStyle>
</ListView>

1 个答案:

答案 0 :(得分:1)

首先,我想我已经弄清楚了为什么你的命令没有被解雇。

由于您在模板中,因此DataContext已更改。因此,您的CommandBinding应如下所示:

AFHTTPSessionManager.h

此外,您的CommandParameter不应该是<i:InvokeCommandAction Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}, Path=DataContext.OnCategorySelected}" CommandParameter="{Binding}"/> ,因为您最终只会获得一个字符串而不是整个Object。

如果您使用我的上述代码,您将获得整个Name。在所述CollectionViewGroup中,您将找到该组中的所有项目。如果您只获得组名,则可以继续执行课程。

我真的不明白你为什么要使用CollectionViewGroup以及它做了什么(因为你没有发布那个代码),但如果你对如何在这样的ContextMenu中显示分组的项目感兴趣,你可以这样做:

ContextMenu

由于我们现在知道,我们必须处理什么(它仍然是<Expander IsExpanded="False" Background="#FFEBEBEB" BorderThickness="0" > <Expander.ContextMenu> <ContextMenu ItemsSource="{Binding Items}"/> </Expander.ContextMenu> </Expander> )我们可以将它的项目设置为ContextMenu的ItemsSource。

希望这有帮助!