How can I display different ContextMenus in WPF ListView GridView?

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

eg. 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 StackPanel (hopefully) and my uncle's Bob.

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 corresponding icon for the listViewItem category.

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

also:

Currently, every ListViewItem is supported by a viewModel - all categories use the same viewModel class.

Background:

The reason I want to display a different ContextMenu instead of changing the ContextMenu is because I am using Prism and the ContextMenus will be scopes auto-populated by different modules.

+2


source to share


1 answer


I think you can do this with the ItemTemplateSelector, instead of setting the ItemTemplate property on the 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 .



0


source







All Articles