C# WPF资源覆盖

C# WPF资源覆盖,c#,wpf,xaml,C#,Wpf,Xaml,我有一个关于xaml wpf中的样式的问题 我有一个适用于所有对象的默认样式。但是对于其中的一些,我想设置第二种样式来覆盖一些属性 现在,如果我给我的第二个样式一个x:key=“style2”并将其设置为样式,我的第一个默认样式将不被应用,但默认的WPF样式是。在我的第一个默认样式中,我不能/不想更改任何内容 如何修复此行为?要确保仍应用默认样式,请添加 BasedOn={StaticResource ResourceKey={x:Type ControlType}} 其中,ControlTy

我有一个关于xaml 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>


这将使用默认样式
这就是专业化

要确保仍应用默认样式,请添加

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>


这将使用默认样式
这就是专业化