0

我的申请中有几个StackPanels希望他们的孩子应用某些样式:

<StackPanel.Resources>
    <Style TargetType="TextBlock" BasedOn="{StaticResource SettingLabel}" />
    <Style TargetType="DockPanel" BasedOn="{StaticResource SettingRow}" />
    <Style TargetType="CheckBox" BasedOn="{StaticResource SettingCheckBox}" />
    <Style TargetType="PasswordBox" BasedOn="{StaticResource DialogPasswordBox}" />
    <Style TargetType="TextBox" BasedOn="{StaticResource DialogTextBox}" />
</StackPanel.Resources>

而不是一遍又一遍地写这 5 行,我想给它StackPanel自己一种风格来应用这些风格,从而减少冗余。

无法Resources在样式设置器中进行设置,因为它没有依赖属性:

<Style x:Key="SettingPanel" TargetType="StackPanel">
    <Setter Property="Resources">
        <Setter.Value>
            <Style TargetType="TextBlock" BasedOn="{StaticResource SettingLabel}" />
            <Style TargetType="DockPanel" BasedOn="{StaticResource SettingRow}" />
            <Style TargetType="CheckBox" BasedOn="{StaticResource SettingCheckBox}" />
            <Style TargetType="PasswordBox" BasedOn="{StaticResource DialogPasswordBox}" />
            <Style TargetType="TextBox" BasedOn="{StaticResource DialogTextBox}" />
        </Setter.Value>
    </Setter>
</Style>

那么有没有其他方法可以做到这一点,而不必为每个孩子设置样式并重复分配样式?

4

1 回答 1

4

Style.Resources您可以在of中定义样式StackPanel。这些将应用于StackPanel使用 SettingPanel 作为样式的所有子项。

<Style x:Key="SettingPanel" TargetType="StackPanel">
    <Style.Resources>
            <Style TargetType="TextBlock" BasedOn="{StaticResource SettingLabel}" />
            <Style TargetType="DockPanel" BasedOn="{StaticResource SettingRow}" />
            <Style TargetType="CheckBox" BasedOn="{StaticResource SettingCheckBox}" />
            <Style TargetType="PasswordBox" BasedOn="{StaticResource DialogPasswordBox}" />
            <Style TargetType="TextBox" BasedOn="{StaticResource DialogTextBox}" />
    </Style.Resources>
</Style>
于 2016-04-13T06:29:13.990 回答