开发者

How can set property in trigger, if it's null?

开发者 https://www.devze.com 2023-02-16 10:19 出处:网络
How can set property in trigger, if it\'开发者_如何学运维s null? <Style.Triggers> <Trigger Property=\"ContextMenu\" Value=\"{x: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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号