views:

470

answers:

1

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.

A: 

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.

TabbyCool