将样式应用于特定类型的所有子元素 [英] Apply Style to all child-elements of specific type

查看:33
本文介绍了将样式应用于特定类型的所有子元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为 wpf 编写一个样式,其中 StatusBar(具有定义的样式)中的所有按钮都具有相同的样式(例如宽度).

I want to write a style for wpf where all buttons in a StatusBar (that has a defined style) have the same style (e.g. width).

这是我的风格:

<Style TargetType="{x:Type StatusBar}"
               x:Key="DialogBoxStatusBarStyle">
    <Setter Property="Background"
                    Value="LightGray" />
    <Setter Property="Padding"
                    Value="5" />

    ...?

</Style>

以及元素的 xaml:

And the xaml for the elements:

<StatusBar Style="{StaticResource ResourceKey=DialogBoxStatusBarStyle}" Grid.Row="3"
               FlowDirection="RightToLeft">
        <Button Content="Übernehmen"
                Width="100"
                HorizontalAlignment="Right" />
        <Button Content="Abbrechen"
                Width="100"
                HorizontalAlignment="Right" />

        <Button Content="OK"
                Width="100"
                HorizontalAlignment="Right" />
    </StatusBar>

在最终版本中,我不想将所有按钮的宽度设置为 100.这应该在 StatusBar 的样式中定义,或者更好地说在 StatusBar 的按钮子项的样式中.

In the final version I don't want to set width to 100 for all buttons. This should be defined in the style of the StatusBar or better say in the style of the button-childs of the StatusBar.

推荐答案

您可以将按钮的默认样式添加到您的 DialogBoxStatusBarStyleResources 中:

You could add a default Style for Buttons to the Resources of your DialogBoxStatusBarStyle:

<Style TargetType="StatusBar" x:Key="DialogBoxStatusBarStyle">
    <Style.Resources>
        <Style TargetType="Button">
            <Setter Property="Width" Value="100"/>
        </Style>
    </Style.Resources>
    ...
</Style>

这篇关于将样式应用于特定类型的所有子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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