Wpf 扩展样式,而不在基础样式中设置x:Key属性

Wpf 扩展样式,而不在基础样式中设置x:Key属性,wpf,Wpf,我有一个文件MyButtonStyles.xaml,它设计了WPF按钮。此文件使用样式设置一些颜色和字体: <ResourceDictionary xmlns......> <Style BasedOn="{StaticResource {x:Type Button}} TargetType="{x:Type Button}"> <Setter Property="Foreground" Value="Blue" />

我有一个文件MyButtonStyles.xaml,它设计了WPF按钮。此文件使用样式设置一些颜色和字体:

<ResourceDictionary xmlns......>
    <Style BasedOn="{StaticResource {x:Type Button}} TargetType="{x:Type Button}">
        <Setter Property="Foreground" Value="Blue" />
        <Setter Property="FontSize" Value="22" />
    </Style>
</ResourceDictionary>
但为此,我必须在基本样式中添加一个
x:Key
属性(ButtonStylesOrig)。这意味着在使用按钮的第一个xaml中,将不再应用基本样式


是否可以扩展样式而不丢失其全局范围(例如,不使用
x:Key
)?

您不能将同一类型的多个默认样式组合到单个资源范围中。但是,可以在嵌套的资源范围中构建默认样式

假设您将
MyButtonStyles.xaml
合并到
App.xaml
资源中。然后,您可以将带有附加setter的第二个样式放入
窗口。参考资料
或其他更深层次的嵌套参考资料中,它将组合正确的隐式样式

一个更本地化的示例:

<Grid>
    <Grid.Resources>
        <ResourceDictionary Source="MyButtonStyles.xaml"/>
    </Grid.Resources>
    <Grid>
        <Grid.Resources>
            <Style BasedOn="{StaticResource {x:Type Button}}" TargetType="{x:Type Button}">
                <Setter Property="Background" Value="Green" />
            </Style>
        </Grid.Resources>
        <Button VerticalAlignment="Top" Margin="20">Both styles</Button>
    </Grid>
    <Button VerticalAlignment="Center" Margin="20">ExampleDictionary style</Button>
</Grid>

两种风格
示例字典样式

不能将同一类型的多个默认样式组合到单个资源范围中。但是,可以在嵌套的资源范围中构建默认样式

假设您将
MyButtonStyles.xaml
合并到
App.xaml
资源中。然后,您可以将带有附加setter的第二个样式放入
窗口。参考资料
或其他更深层次的嵌套参考资料中,它将组合正确的隐式样式

一个更本地化的示例:

<Grid>
    <Grid.Resources>
        <ResourceDictionary Source="MyButtonStyles.xaml"/>
    </Grid.Resources>
    <Grid>
        <Grid.Resources>
            <Style BasedOn="{StaticResource {x:Type Button}}" TargetType="{x:Type Button}">
                <Setter Property="Background" Value="Green" />
            </Style>
        </Grid.Resources>
        <Button VerticalAlignment="Top" Margin="20">Both styles</Button>
    </Grid>
    <Button VerticalAlignment="Center" Margin="20">ExampleDictionary style</Button>
</Grid>

两种风格
示例字典样式
这项工作:

<Window x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="MyButtonStyles.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
    <Grid>
        <Grid.Resources>
            <Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
                <Setter Property="Background" Value="Green" />
            </Style>
        </Grid.Resources>

        <Button Content="Button" />
    </Grid>
</Window>

关键是不要覆盖将基本样式合并到的同一资源字典中的资源:

这项工作:

<Window x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="MyButtonStyles.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
    <Grid>
        <Grid.Resources>
            <Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
                <Setter Property="Background" Value="Green" />
            </Style>
        </Grid.Resources>

        <Button Content="Button" />
    </Grid>
</Window>

关键是不要覆盖将基本样式合并到的同一资源字典中的资源:


那么您的第一种风格需要一把钥匙。您只能有一个默认样式。您的意思是:当我向第一个样式添加键时,我的第二个样式将成为新的默认样式?是的,默认样式总是没有键的样式。您的假设是错误的,不可能有一个没有键的样式。如果您不定义它,它仍然有一个默认设置为key=“{x:Type Button}”的键。如果要在第一个样式上使用默认键,请更改BasedOn=“{x:Type Button}”的BasedOn属性。请参见备注:那么您的第一个样式需要一个键。您只能有一个默认样式。您的意思是:当我向第一个样式添加键时,我的第二个样式将成为新的默认样式?是的,默认样式总是没有键的样式。您的假设是错误的,不可能有一个没有键的样式。如果您不定义它,它仍然有一个默认设置为key=“{x:Type Button}”的键。如果要在第一个样式上使用默认键,请更改BasedOn=“{x:Type Button}”的BasedOn属性。见备注:这很有趣。如果某个特定范围的默认样式都发生在一个文件中,则扩展该范围的默认样式是有效的。但是如果我为整个应用程序定义了一个默认样式,并且我想在一个特定的文件中扩展它,这是不可能的?这取决于。。。文件不是XAML资源作用域的良好描述。。。如果将一个特定文件合并到与整个应用程序样式相同的范围中,那么它将不起作用。如果您将一个特定的文件合并到某个嵌套框架元素的资源中,那么它就会工作。如果某个特定范围的默认样式都发生在一个文件中,则扩展该范围的默认样式是有效的。但是如果我为整个应用程序定义了一个默认样式,并且我想在一个特定的文件中扩展它,这是不可能的?这取决于。。。文件不是XAML资源作用域的良好描述。。。如果将一个特定文件合并到与整个应用程序样式相同的范围中,那么它将不起作用。如果您将一个特定文件合并到某个嵌套框架元素的资源中,那么它将起作用。