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

发布于 2024-08-04 08:02:28 字数 2223 浏览 2 评论 0原文

我有一个 ListView GridView 和 ListViewItems 代表不同类别的项目。我想为每个类别的项目显示不同的上下文菜单。我希望使用 DataTemplates 来做到这一点,但我很挣扎。我的 TreeView 每个类别都有一个 DataTemplate,我可以看到如何为每个类别设置不同的 ContextMenu,但我似乎无法获得类似的 DataTemplates 来为我的 ListView 工作。我是不是找错了树?

例如,这是我的 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>

我可以将我的 ContextMenu 添加到 StackPanel(我希望),而 Bob 是我的叔叔。

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

DataTemplate 资源中的 imageConverter 显示 listViewItem 类别的相应图标。

我不知道从哪里开始。那么,首先,我想做的事情可能吗?如果是这样,请让我开始吧。

另外:

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

背景:

我想显示不同的 ContextMenu 而不是更改 ContextMenu 的原因是我正在使用 Prism,并且 ContextMenus 将是由各种模块自动填充的区域。

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?

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.

But the guts of the GridView looks like this:

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

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

香草可樂 2024-08-11 08:02:28

我认为您可以使用 ItemTemplateSelector 来完成此操作,而不是在 ListView 上设置 ItemTemplate 属性,而是使用 ItemTemplateSelector 属性。您必须创建自己的 ItemTemplateSelector 类实现并定义逻辑,以便它知道针对每组条件使用哪个模板,然后您只需要创建一组模板就可以了! 这里有一个关于如何执行此操作的很好的教程。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文