2

I have a ContextMenu in the App.xaml that looks like this:

<ContextMenu x:Key="TreeContextMenuTest" ItemsSource="{Binding ContextMenu}">....

Then I have a ListView, whose items are styled in Themes/Generic.xaml. It looks like this (I have deleted the irrelevant things. The style is applied properly and my question is only about how to attach the context menu)

<Style TargetType='{x:Type ListViewItem}'>
    <Setter Property="ContextMenu" Value="{StaticResource TreeContextMenuTest}" />
</Style>

However, I do get DependencyProperty.UnsetValue is not a valid value for property ContextMenu error. Any ideas?

Peter
  • 249
  • 3
  • 11

2 Answers2

3

As mentioned in comment instead of StaticResource use DynamicResource -

<Setter Property="ContextMenu" Value="{DynamicResource TreeContextMenuTest}" />

Refer to this for reference - StaticResource vs DynamicResource

Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
  • This has also worked for me, allthough I don't get, how the `StaticResource` can't handle this. Isn't the `ContextMenu`defined at the very start of the Application? - In my Case, my Context Menus are defined in *ContextMenus.xaml* which is loaded by *Generic.xaml* by the Applications Resources. So how can a Style definded later than the ContenxtMenu Resource not know about it? This does work with Image Resources, so whats the deal with contextmenus? – LuckyLikey Oct 20 '17 at 09:15
0

I've tested your solution and it seems to be in order:

MainWindow.xaml:

<Window x:Class="WpfApplication3.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid.Resources>
        <Style TargetType='{x:Type ListViewItem}'>
            <Setter Property="ContextMenu" Value="{StaticResource TreeContextMenuTest}" />
        </Style>
    </Grid.Resources>
    <ListView>
        <ListViewItem>aaa</ListViewItem>
        <ListViewItem>bbb</ListViewItem>
        <ListViewItem>ccc</ListViewItem>
    </ListView>
</Grid>

App.xaml:

<Application x:Class="WpfApplication3.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         StartupUri="MainWindow.xaml">
<Application.Resources>
    <ContextMenu x:Key="TreeContextMenuTest" ItemsSource="{Binding ContextMenu}"/>

</Application.Resources>

App.xaml.cs:

public partial class App : Application
{
    public ObservableCollection<MenuItem> ContextMenu { get; set; }

    public App()
    {
        ContextMenu = new ObservableCollection<MenuItem>();
        var mi = new MenuItem {Name = "Test"};
        ContextMenu.Add(mi);
    }
}

I don't know the way you used to populate your context menu items and I suggest rather then doing it in binding, do it in the xaml file, but a right click menu Item appeared, and if I changed it to standard population of menuItems, they also appeared properly.

Edit: when saying standard population I meant as explained here:

http://wpftutorial.net/ContextMenu.html

Ron.B.I
  • 2,726
  • 1
  • 20
  • 27