I have a style that doesn't seem to be working. In spite of Snoop telling me the DataContext
for the ListBoxItem
is correct, nothing shows up. If it was a problem with the binding for Commands
I would expect to see an empty context menu appear.
The style:
<ContextMenu x:Key="CommandsContextMenu" ItemsSource="{Binding Commands}">
<Style TargetType="MenuItem">
<Setter Property="Header" Value="{Binding Name}"/>
</Style>
</ContextMenu>
<Style TargetType="ListBoxItem">
<Setter Property="ContextMenu" Value="{StaticResource CommandsContextMenu}" />
<Style.Triggers>
<DataTrigger Value="True">
<DataTrigger.Binding>
<Binding Path="DataContext.HasCommands" />
</DataTrigger.Binding>
</DataTrigger>
</Style.Triggers>
</Style>
The snoop DataContext:
The snoop properties showing the ContextMenu property isn't even set.
The idea here, was that without knowing any of the types, I could have a listbox item style where if the thing it was bound to has a property called HasCommands, and it was set to true, then it would set a context menu on that listbox item, bound to the Commands property.
I'm not getting any binding errors or warnings from PresentationTraceSources.DataBindingSource
Why doesn't the context menu get set?
<ContextMenu><ContextMenu.ItemContainerStyle><Style TargetType="MenuItem" .../> ...
– 15ee8f99-57ff-4f92-890c-b56153DataContext
. See this question for a few ways to work around it. stackoverflow.com/questions/15033522/… – Bradley UffnerContextMenu
is defined as a resource. By default, only one instance is created and then returned upon every fetch from the dictionary, resulting in this single instance being set as the context menu of multiple elements, which I think might be the cause of your problems. Try settingx:Shared="False"
on it in order to ensure a new instance is created every time the resource is fetched from the dictionary. Also, @BradleyUffner comment is useful and probably it'll be the next problem you'll encounter. – Grx70