如何以全局样式覆盖ContextMenu? [英] How to override ContextMenu in global style?

查看:83
本文介绍了如何以全局样式覆盖ContextMenu?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在库中声明了我控件的样式

  < ContentControl.Resources> 
< ContextMenu x:Key = ContextMenu>
< MenuItem Header = {x:Static Drawing:Headers.AddEdge} Click = AddEdgeClick />
< MenuItem Header = {x:Static Drawing:Headers.ChangeID} Click = ChangeIDClick />
< MenuItem Header = {x:Static Drawing:Headers.Remove} Click = RemoveClick />
< / ContextMenu>
< Style x:Key = Style TargetType = {x:Type绘图:Node}>
< Setter Property = ContextMenu Value = {StaticResource ContextMenu} />
< / Style>
< /ContentControl.Resources>

< ContentControl.Style>
< StaticResource ResourceKey = Style />
< /ContentControl.Style>

但是应用程序中此控件的全局样式无效...

 < Style TargetType = Drawing:Node> 
< Setter Property = ContextMenu Value = {x:Null} />
< EventSetter Event = MouseLeftButtonUp Handler = DirectoryClicked />
< / Style>


解决方案

如果您在元素上明确设置了Style属性,那么任何隐式样式(即全局样式)都不会应用。如果您的全局样式实际上是默认样式,那么仍然应该应用它,但这听起来不像您在做什么。



您可以基于显式样式

 < Style x:Key = Style TargetType = {x:Type Drawing:Node} BasedOn = {StaticResource {x:Type Drawing:Node}}> 
< Setter Property = ContextMenu Value = {StaticResource ContextMenu} />
< / Style>

当然,这是假定Drawing:Node与GraphNode:Node具有相同的类/类型。 / p>

I declare the style of my control in library:

<ContentControl.Resources>
    <ContextMenu x:Key="ContextMenu">
        <MenuItem Header="{x:Static Drawing:Headers.AddEdge}"  Click="AddEdgeClick"/>
        <MenuItem Header="{x:Static Drawing:Headers.ChangeID}" Click="ChangeIDClick"/>
        <MenuItem Header="{x:Static Drawing:Headers.Remove}"   Click="RemoveClick"/>
    </ContextMenu>
    <Style x:Key="Style" TargetType="{x:Type Drawing:Node}">
        <Setter Property="ContextMenu" Value="{StaticResource ContextMenu}"/>
    </Style>
</ContentControl.Resources>

<ContentControl.Style>
    <StaticResource ResourceKey="Style"/>
</ContentControl.Style>

But global style for this control in application doesn't work...

<Style TargetType="Drawing:Node">
    <Setter Property="ContextMenu" Value="{x:Null}"/>
    <EventSetter Event="MouseLeftButtonUp" Handler="DirectoryClicked"/>
</Style>

解决方案

If you explicitly set the Style property on an element, then any implicit Styles (i.e. your global style) will not be applied. If your global style is actually the default Style, then it should still be applied, but it doesn't sounds like what you are doing.

You can base your explicit Style on your implicit Style though, like so:

<Style x:Key="Style" TargetType="{x:Type Drawing:Node}" BasedOn="{StaticResource {x:Type Drawing:Node}}">
    <Setter Property="ContextMenu" Value="{StaticResource ContextMenu}"/>
</Style>

This is of course assuming that Drawing:Node is the same class/type as GraphNode:Node.

这篇关于如何以全局样式覆盖ContextMenu?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆