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>