Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF通过父样式自动将样式指定给子样式_C#_Wpf_Xaml_Xaml Resources - Fatal编程技术网

C# WPF通过父样式自动将样式指定给子样式

C# WPF通过父样式自动将样式指定给子样式,c#,wpf,xaml,xaml-resources,C#,Wpf,Xaml,Xaml Resources,我的应用程序中有几个stackpanel,希望他们的孩子应用某些样式: <StackPanel.Resources> <Style TargetType="TextBlock" BasedOn="{StaticResource SettingLabel}" /> <Style TargetType="DockPanel" BasedOn="{StaticResource SettingRow}" /> <Style TargetT

我的应用程序中有几个
stackpanel
,希望他们的孩子应用某些样式:

<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>

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

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

<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>

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

<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>