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

时间:2018-06-23 03:29:17

标签: c# wpf xaml resources styles

我正在使用以下资源将Grid控件样式化为表格标题:

<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>

1 个答案:

答案 0 :(得分:1)

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

<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>

enter image description here

如果您希望在整个应用中使用多个ResourceDictionary's,则可以使用另一种方法;但使用相同的内部键,则是每个元素将引用唯一的ResourceDictionary。这将不使用App.xaml资源,而是直接指向应用程序中的文件位置。由于ResourceDictionary's的默认“生成操作”为“页面”,因此它将以这种方式引用位置。如果您的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>

看起来像:

enter image description here