Wpf 自动样式继承
我有一个关于自动样式继承的问题 基本上,我有一个超类,我想为它定义文本框的默认样式,并为从它派生的类定义文本块。这些样式基于styles.xaml中定义的默认样式Wpf 自动样式继承,wpf,Wpf,我有一个关于自动样式继承的问题 基本上,我有一个超类,我想为它定义文本框的默认样式,并为从它派生的类定义文本块。这些样式基于styles.xaml中定义的默认样式 <controls:BaseUserControl.Resources> <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource DisplayLabel}"> <Setter Property="Foreg
<controls:BaseUserControl.Resources>
<Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource DisplayLabel}">
<Setter Property="Foreground" Value="Black"/>
<Setter Property="Margin" Value="10,0,10,0"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource InputField}">
<Setter Property="Height" Value="30"/>
<Setter Property="Margin" Value="10,0,0,0"></Setter>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"></Setter>
</Style>
</controls:BaseUserControl.Resources>
我忘记了什么
基本上,我不想显式地将每个textblock和textbox的样式设置为某个键。您需要将样式放置在ResourceDictionary中,并使用ResourceDictionary.MergedDictionaries属性将其包含到超类和派生类中 样式文件Styles.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" ....>
<Style ...> <!--Your styles goes here-->
</ResourceDictionary>
控制父项和子项:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>