WPF使用UIElement为所有元素设置样式

3

我想要做的事情是:

<Application.Resources>
    <Style TargetType="{x:Type UIElement}">
        <Setter Property="Opacity"
                Value=".1" />
    </Style>
</Application.Resources>

我希望能够为任何类型(不仅限于某些最终的具体UI类型)设置样式。我并不寻求最佳实践,这更像是一个需要思考的问题。

我注意到WPF不会给TargetType中指定的任何超类(如UIElementFrameworkElement等等)设置样式。只有当TargetType等同于具体的UI类(如ButtonRectangle)时才会设置样式。

1个回答

2
如果您只想定义一个基本样式,可以使用BasedOn属性。
<Style TargetType="FrameworkElement" x:Key="ElementBase">
    <Setter Property="Height" Value="24"/>
</Style>

<Style TargetType="TextBlock" BasedOn="{StaticResource ElementBase}">
</Style>

这需要花费一点时间,但也许会有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接