如何在WPF ListView GridView中显示不同的contextmenu ?

时间:2021-06-19 21:24:25

I have a ListView GridView with ListViewItems that represent different categories of items. I'd like to display a different ContextMenu for each category of item. I was hoping to do this using DataTemplates but I'm struggling. My TreeView has a DataTemplate per category and I can see how I can set a different ContextMenu for each there but I can't seem to get similar DataTemplates to work for my ListView. Am I barking up the wrong tree?

我有一个ListViewItems的ListView GridView,它表示不同类别的项目。我想为每个类别的项目显示不同的ContextMenu。我曾希望使用datatemplate来实现这一点,但我正在努力。我的TreeView每个类别都有一个DataTemplate,我可以看到如何为每个目录设置不同的ContextMenu,但我似乎不能得到类似的DataTemplate来为我的ListView工作。我找错人了吗?

E.g. this is one of my DataTemplates for the TreeView:

这是我为树景拍摄的照片之一:

<DataTemplate DataType="{x:Type viewModel:Cat1ViewModel}">
    <StackPanel Orientation="Horizontal">
        <Image Width="16" Height="16" Margin="3,0" 
               Source="..\Images\cat1.png"/>
        <TextBlock Text="{Binding Name}" />
    </StackPanel>
</DataTemplate>

and I can add my ContextMenu to the StackPanel (I hope) and Bob's my uncle.

我可以把我的ContextMenu添加到StackPanel(我希望)和Bob的叔叔。

But the guts of the GridView looks like this:

但GridView的核心是这样的:

<ListView.Resources>
    <DataTemplate x:Key="image">
        <Image Width="16" Height="16" Margin="-3,0,-3,0"
                          HorizontalAlignment="Center"
                          Source="{Binding Path=ObjectClass, 
                                           Converter={StaticResource imageConverter}}" />
    </DataTemplate>
</ListView.Resources>


<ListView.View>
    <GridView>
        <GridViewColumn Width="20"
                        CellTemplate="{StaticResource image}"/>
        <GridViewColumn Width="140" Header="Name"
                        DisplayMemberBinding="{Binding Path=Name}"
                        infrastructure:GridViewSort.PropertyName="Name"/>
        <GridViewColumn Width="140" Header="Type" 
                        DisplayMemberBinding="{Binding Path=Category}"
                        infrastructure:GridViewSort.PropertyName="Category"/>
        <GridViewColumn Width="400" Header="Description"
                        DisplayMemberBinding="{Binding Path=Description}"
                        infrastructure:GridViewSort.PropertyName="Description"/>
    </GridView>
</ListView.View>

This imageConverter in the DataTemplate resource displays the appropriate icon for the category of the listViewItem.

DataTemplate资源中的这个imageConverter显示listViewItem类别的适当图标。

I'm not sure where to start. So, first, is what I want to do possible? If so, can you get me started, please.

我不知道从哪里开始。首先,我想要做的是可能的吗?如果是的话,你能帮我开始吗?

Also:

另外:

At the moment, each ListViewItem is backed by a viewModel - all categories use the same viewModel class.

目前,每个ListViewItem都支持一个viewModel——所有类别都使用相同的viewModel类。

Background:

背景:

The reason I want to display a different ContextMenu rather than changing the ContextMenu is that I'm using Prism and the ContextMenus will be Regions populated automatically by various modules.

我想要显示不同的ContextMenu,而不是更改ContextMenu的原因是,我使用Prism,而ContextMenu将是由各个模块自动填充的区域。

1 个解决方案

#1


0  

I think you can do this with an ItemTemplateSelector, rather than setting the ItemTemplate property on your ListView, use the ItemTemplateSelector property. You have to create your own implementation of the ItemTemplateSelector class and define the logic so that it knows which template to use for each set of conditions, then you just need to create a set of templates and you should be good to go! There's a good tutorial on how to do this here.

我认为您可以使用ItemTemplateSelector来实现这一点,而不是在ListView上设置ItemTemplate属性,而是使用ItemTemplateSelector属性。您必须创建自己的ItemTemplateSelector类的实现,并定义逻辑,这样它就知道要为每一组条件使用哪个模板,然后您只需创建一组模板,您就可以开始了!这里有一个很好的教程。

#1


0  

I think you can do this with an ItemTemplateSelector, rather than setting the ItemTemplate property on your ListView, use the ItemTemplateSelector property. You have to create your own implementation of the ItemTemplateSelector class and define the logic so that it knows which template to use for each set of conditions, then you just need to create a set of templates and you should be good to go! There's a good tutorial on how to do this here.

我认为您可以使用ItemTemplateSelector来实现这一点,而不是在ListView上设置ItemTemplate属性,而是使用ItemTemplateSelector属性。您必须创建自己的ItemTemplateSelector类的实现,并定义逻辑,这样它就知道要为每一组条件使用哪个模板,然后您只需创建一组模板,您就可以开始了!这里有一个很好的教程。