How can set property in trigger, if it's null?
How can set property in trigger, if it'开发者_如何学运维s null?
<Style.Triggers>
<Trigger Property="ContextMenu" Value="{x:Null}">
<Setter Property="ContextMenu" Value="{DynamicResource ContextMenu}"/>
</Trigger>
</Style.Triggers>
You can simply set the ContextMenu directly in your Style like so:
<Style ...>
<Setter Property="ContextMenu" Value="{DynamicResource ContextMenu}"/>
</Style>
If the user sets the ContextMenu explicitly, then it will take precedence over the Style setter. See this MSDN article for the order or precedence. Your Style setter falls under #8, while a user setting is at #3.
Have you tried?
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=ContextMenu}" Value="{x:Null}">
<Setter Property="ContextMenu" Value="{DynamicResource ContextMenu}"/>
</DataTrigger>
</Style.Triggers>
But in reality you should be setting the context menu in the control's style and then any user of the control can override it in a derived style or in the control attributes.
精彩评论