Question

As shown in the XAML below, I have a ListView with a label in it; And in that label is a ContextMenu. The ContextMenu is in a DataTemplate and resources by an ItemTemplate of the ListView. Secondly, I have a DisplayMemberPath property which links to a method in one of my classes.

 <ListView x:Name="TitleList" ItemsSource="{Binding Collections}" ItemTemplate="{DynamicResource Template}" BorderBrush="{x:Null}" DisplayMemberPath="Title">
            <ListView.ItemContainerStyle>
            <Style>
                <Setter Property="FrameworkElement.Margin" Value="20,20,0,0"/>
            </Style>
            </ListView.ItemContainerStyle>
            <ListView.Resources>
                <DataTemplate x:Key="Template">
                    <Label Content="{Binding .}" Width="500" HorizontalAlignment="Left">
                        <Label.ContextMenu>
                            <ContextMenu>
                                <MenuItem Header="Add Question" Click="AddQuestion"/>
                                <MenuItem Header="Edit Title"/>
                                <MenuItem Header="Delete Title"/>
                                <MenuItem Header="Reposition Title"/>
                            </ContextMenu>
                        </Label.ContextMenu>
                    </Label>
                </DataTemplate>
                </ListView.Resources>
        </ListView>

The problem I am having is that DisplayMemberPath and ItemTemplate can not both be properties of my ListView at once, yet I need both.

Is there another way to have a ContextMenu without using ItemTemplate, or is there another solution?

EDIT:

Been trying to complete this and have tried changing these:

<ListView x:Name="TitleList" ItemsSource="{Binding Collections}" ItemTemplate="{DynamicResource Template}" BorderBrush="{x:Null}" SelectedValuePath="Title">

<ListView x:Name="TitleList" ItemsSource="{Binding Collections}" ItemTemplate="{DynamicResource Template}" BorderBrush="{x:Null}" TextSearch.TextPath="Title">

Both of these let me right click and it will display the menu, but the string shows up as MVVMModel.NewTitleClass.

Was it helpful?

Solution

Remove the DisplayMemberPath. You are using a custom template for how a ListViewItem should appear. The issue is with your label's Content binding. Change it to this (or tinker around with it a bit if this doesn't work):

Content="{Binding Title}"

Another option is that when you don't specify the property to display when binding a control, it uses the ToString() result. If you haven't overridden this, it will show the fully qualified class name. So you could override the ToString() like this:

public override ToString()
{
   return Title;
}

The first option would be the best choice though.

OTHER TIPS

... I need both ...

No, you do not

Simply add an extra element in your DataTemplate to display the Title of each item.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top