The DataContext for my window is an IDictionary>.
Can anyone explain to me why this works fine:
<Style x:Key="MenuItemStyle">
<Setter Property="MenuItem.Header" Value="{Binding Ticker}"/>
</Style>
<Style x:Key="ContextMenuStyle">
<Setter Property="MenuItem.Header" Value="{Binding Key}"/>
<Setter Property="MenuItem.ItemsSource" Value="{Binding Value}"/>
<Setter Property="MenuItem.ItemContainerStyle" Value="{StaticResource MenuItemStyle}"/>
</Style>
<ContextMenu ItemContainerStyle="{StaticResource ContextMenuStyle}" ItemsSource="{Binding Quotes}" />
But this does not:
<Style TargetType="{x:Type ContextMenu}" x:Key="ContextMenuStyle">
<Setter Property="MenuItem.Header" Value="{Binding Key}"/>
<Setter Property="MenuItem.ItemsSource" Value="{Binding Value}"/>
<Setter Property="MenuItem.ItemContainerStyle">
<Setter.Value>
<Style TargetType="{x:Type MenuItem}">
<Setter Property="Header" Value="{Binding Ticker}"/>
</Style>
</Setter.Value>
</Setter>
</Style>
<ContextMenu ItemContainerStyle="{StaticResource ContextMenuStyle}" ItemsSource="{Binding Quotes}" />
EDIT: if i remove T’argetType=”{x:Type ContextMenu}”‘, it works fine. i don’t clearly understand why however.
thanks
You need to change the value of the
TargetTypefromContextMenutoMenuItem. TheItemContainerStyleproperty of theContextMenusets the style to the container element of yourContextMenu, in this case aMenuItem. However you set the target type of the style toContextMenuso it cannot work. See this link for more details.