WPF:可通过密钥访问的全局资源

WPF: Global resource accessible by key

我将 Grid 控件的样式设置为 table headers 使用如下资源:

<Grid.Resources>
    <Style TargetType="TextBlock">
        <Setter Property="Padding" Value="5,10" />
        <Setter Property="Foreground" Value="{StaticResource ForegroundDarkBrush}" />
        <Setter Property="FontWeight" Value="Bold" />
        <Setter Property="HorizontalAlignment" Value="Center" />
    </Style>
    <Style TargetType="Border">
        <Setter Property="BorderThickness" Value="0.0,1.0,0.0,0" />
        <Setter Property="BorderBrush" Value="{StaticResource ForegroundDarkBrush}" />
        <Setter Property="Background" Value="{StaticResource BackgroundLightBrush}" />
    </Style>
</Grid.Resources>

问题是,我需要将该资源应用到我的应用程序的多个位置,这导致代码重复。

我想知道是否可以将资源存储在我的 App.xaml 中并通过密钥或类似方式使用它们?像这样:

<Resources Key="MyResourceSet">
     <Style>
         [..]
     </Style>
</Resources>

<Grid Resource="MyResourceSet">
[...]
</Grid>

像在任何其他 UIElement 中一样将样式放置在 App.Resources 中。

<Application x:Class="Question_Answer_WPF_App.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>

        <Style x:Key="MyButtonStyle"
               TargetType="Button">
            <Setter Property="Background"
                    Value="Green" />
            <Setter Property="Height"
                    Value="30" />
            <Setter Property="Width"
                    Value="100" />
        </Style>

    </Application.Resources>
</Application>

在您的应用程序中的任意位置引用。

<Window x:Class="Question_Answer_WPF_App.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow"
        Height="450"
        Width="800">

    <Button Content="Testing"
            Style="{StaticResource MyButtonStyle}" />

</Window>

如果您希望在整个应用程序中使用多个 ResourceDictionary's,则可以使用另一种方法;但使用相同的内部键,是引用将使用它的每个元素的唯一 ResourceDictionary。这不会使用 App.xaml 资源,但会直接指向应用程序中的文件位置。由于 ResourceDictionary's 的默认值 'Build Action' 为 'Page',因此可以通过这种方式引用位置。如果您的 ResourceDictionary 无法以这种方式工作,第一件事是通过右键单击解决方案资源管理器中的 ResourceDictionary 来检查它并确保它是正确的。

示例:

MyCustomResourcesA.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style TargetType="TextBlock">
        <Setter Property="FontSize"
                Value="46" />
    </Style>

    <Style x:Key="MyButtonStyle"
           TargetType="Button">
        <Setter Property="Background"
                Value="Green" />
        <Setter Property="Height"
                Value="30" />
        <Setter Property="Width"
                Value="100" />
    </Style>

</ResourceDictionary>

MyCustomResourcesB.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:Question_Answer_WPF_App">

    <Style TargetType="TextBlock">
        <Setter Property="FontSize"
                Value="26" />
    </Style>

    <Style x:Key="MyButtonStyle"
           TargetType="Button">
        <Setter Property="Background"
                Value="Blue" />
        <Setter Property="Height"
                Value="20" />
        <Setter Property="Width"
                Value="200" />
    </Style>

</ResourceDictionary>

MainWindow.xaml

<Window x:Class="Question_Answer_WPF_App.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow"
        Height="450"
        Width="800">

    <StackPanel HorizontalAlignment="Left">

        <StackPanel>
            <StackPanel.Resources>
                <ResourceDictionary Source="MyCustomResourcesA.xaml" />
            </StackPanel.Resources>
            <TextBlock Text="I'm using MyCustomResourcesA" />
            <Button Content="Testing"
                    Style="{StaticResource MyButtonStyle}" />
        </StackPanel>

        <StackPanel>
            <StackPanel.Resources>
                <ResourceDictionary Source="MyCustomResourcesB.xaml" />
            </StackPanel.Resources>
            <TextBlock Text="I'm using MyCustomResourcesB" />
            <Button Content="Testing"
                    Style="{StaticResource MyButtonStyle}" />
        </StackPanel>

    </StackPanel>

</Window>

看起来像: