不适用于儿童的本地元素范围样式

博基贝格

我是 WPF 初学者,我有点想弄清楚为什么下面的 XAML 没有做我认为应该做的事情:

<ToolBar HorizontalAlignment="Left" Margin="255,250,0,0" VerticalAlignment="Top">
    <ToolBar.Resources>
        <Style TargetType="{x:Type Separator}">
            <Setter Property="Margin" Value="4,6" />
        </Style>
    </ToolBar.Resources>
    <Button Content="Save"/>
    <Button Content="Cancel"/>
    <Separator />
    <Button Content="Options"/>
</ToolBar>

这应该会导致<Separator />具有 的边距,4,6但只有在我明确指定x:Key样式和<Separator Style="..." />.

从我到目前为止所学到的, my<Style TargetType="{x:Type Separator}">应该应用于 内部的所有分隔符<ToolBar>、它的子元素、它的子元素等等。

我做错了什么?

毫米8

你应该设置x:Key{x:Static ToolBar.SeparatorStyleKey}了内应用的样式ToolBar

<ToolBar HorizontalAlignment="Left" Margin="255,250,0,0" VerticalAlignment="Top">
    <ToolBar.Resources>
        <Style x:Key="{x:Static ToolBar.SeparatorStyleKey}" TargetType="{x:Type Separator}">
            <Setter Property="Margin" Value="4,6" />
            <Setter Property="Background" Value="Red" />
        </Style>
    </ToolBar.Resources>
    <Button Content="Save"/>
    <Button Content="Cancel"/>
    <Separator />
    <Button Content="Options"/>
</ToolBar>

这是因为ToolBar该类包含一些“特殊”逻辑,用于将默认样式应用于某些类型的控件,包括Separatorhttps : //referencesource.microsoft.com/#PresentationFramework/src/Framework/System/Windows/Controls/ToolBar.cs, 5d1684510f45eeb3

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章