Wpf 如何将样式与现有资源字典合并?
我有这个资源字典:Wpf 如何将样式与现有资源字典合并?,wpf,dictionary,merge,resources,Wpf,Dictionary,Merge,Resources,我有这个资源字典: <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary x:Name="defaultStyles" Source="/ReuxablesLegacy;component/edge.xaml" /
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary x:Name="defaultStyles" Source="/ReuxablesLegacy;component/edge.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
当我定义下面的样式时,它们会覆盖资源字典中的外观,我不希望这样,我希望从上面“合并”或“继承”:
<Grid.Resources>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Margin" Value="0,0,0,10" />
<Setter Property="Width" Value="200" />
</Style>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="Margin" Value="0,0,0,10" />
<Setter Property="Width" Value="200" />
</Style>
<Style TargetType="{x:Type DatePicker}" >
<Setter Property="Margin" Value="0,0,0,10" />
<Setter Property="Width" Value="200" />
</Style>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
</Grid>
我如何使用BasedOn属性或其他东西来实现这一点?如果您的默认样式资源字典中有这样的条目
<!--A Style that affects all TextBlocks-->
<Style TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="FontFamily" Value="Comic Sans MS"/>
<Setter Property="FontSize" Value="14"/>
</Style>
<!--A Style that extends the previous TextBlock Style-->
<!--This is a "named style" with an x:Key of TitleText-->
<Style BasedOn="{StaticResource {x:Type TextBlock}}"
TargetType="TextBlock"
x:Key="TitleText">
<Setter Property="FontSize" Value="26"/>
<Setter Property="Foreground">
<Setter.Value>
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
<LinearGradientBrush.GradientStops>
<GradientStop Offset="0.0" Color="#90DDDD" />
<GradientStop Offset="1.0" Color="#5BFFFF" />
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Style>
…然后你可以实现这样的东西
<!--A Style that affects all TextBlocks-->
<Style TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="FontFamily" Value="Comic Sans MS"/>
<Setter Property="FontSize" Value="14"/>
</Style>
<!--A Style that extends the previous TextBlock Style-->
<!--This is a "named style" with an x:Key of TitleText-->
<Style BasedOn="{StaticResource {x:Type TextBlock}}"
TargetType="TextBlock"
x:Key="TitleText">
<Setter Property="FontSize" Value="26"/>
<Setter Property="Foreground">
<Setter.Value>
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
<LinearGradientBrush.GradientStops>
<GradientStop Offset="0.0" Color="#90DDDD" />
<GradientStop Offset="1.0" Color="#5BFFFF" />
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Style>
这种方法将提供您所追求的继承效果
MSDN参考号位于