1
我有一个关于xaml wpf中的样式的问题。WPF资源覆盖
我有一个应该适用于所有对象的默认样式。但对于其中一些人,我想设置第二种风格来覆盖一些属性。
现在,如果我给我的第二个样式一个x:key =“style2”并将其设置为样式,我的第一个默认样式不适用,但默认WPF样式是。我不能/不想改变我的第一个默认样式
我该如何解决这个行为?
我有一个关于xaml wpf中的样式的问题。WPF资源覆盖
我有一个应该适用于所有对象的默认样式。但对于其中一些人,我想设置第二种风格来覆盖一些属性。
现在,如果我给我的第二个样式一个x:key =“style2”并将其设置为样式,我的第一个默认样式不适用,但默认WPF样式是。我不能/不想改变我的第一个默认样式
我该如何解决这个行为?
为了确保您的默认样式仍然适用,您可以添加
BasedOn={StaticResource ResourceKey={x:Type ControlType}}
哪里ControlType
是默认样式应用到对象的类型。
下面是一个例子:
<Window x:Class="StyleOverrides.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="FontFamily" Value="Comic Sans MS" />
</Style>
<Style x:Key="Specialization"
TargetType="{x:Type TextBlock}"
BasedOn="{StaticResource ResourceKey={x:Type TextBlock}}"
>
<Setter Property="FontStyle" Value="Italic" />
<Setter Property="Foreground" Value="Blue" />
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Viewbox Grid.Row="0" >
<TextBlock>This uses the default style</TextBlock></Viewbox>
<Viewbox Grid.Row="1">
<TextBlock Style="{StaticResource Specialization}">
This is the specialization
</TextBlock>
</Viewbox>
</Grid>
</Window>