美文网首页
解决全局更改Separator的样式在ContextMenu中不

解决全局更改Separator的样式在ContextMenu中不

作者: WangsYi | 来源:发表于2017-06-28 15:57 被阅读0次

今天在编写样式时遇到Separator的样式全局设置后无效问题,经过漫长的搜索,终于找到解决方案,就是重写{x:Static MenuItem.SeparatorStyleKey}样式。
原因是Menu 元素内的 Separator 控件外观与 Menu 外的 Separator 控件不一样。当创建带 Separator 的 Menu 时,该控件会自动应用由 SeparatorStyleKey 属性标识的 Style。

<Style x:Key="{x:Static MenuItem.SeparatorStyleKey}" TargetType="{x:Type Separator}">
    <Setter Property="Foreground" Value="Red"></Setter>
    <Setter Property="Background" Value="Blue"></Setter>
    <Setter Property="Width" Value="45"></Setter>
    <Setter Property="HorizontalAlignment" Value="Center"></Setter>
    <Setter Property="VerticalAlignment" Value="Center"></Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Separator}">
                <Grid>
                    <Rectangle Height="1" Stroke="{TemplateBinding Foreground}" />
                </Grid>

            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

参考:如何:为用作菜单项的 Separator 设置样式

相关文章

网友评论

      本文标题:解决全局更改Separator的样式在ContextMenu中不

      本文链接:https://www.haomeiwen.com/subject/qhvucxtx.html