Categle
Categle

Reputation: 514

WPF: Global resource accessible by key

I'm styling the Grid controls to be the table headers using resources like so:

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

The thing is, I need to apply that resources into multiple places in my app, which leads to the code being repeated.

I was wondering if this is possible to store the resources in my App.xaml and use them by the key or something like that? Like so:

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

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

Upvotes: 0

Views: 1312

Answers (1)

Michael Puckett II
Michael Puckett II

Reputation: 6749

Place the Style in the App.Resources like you would in any other 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>

Reference wherever you want in your app.

<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

Another way to do this if you want several ResourceDictionary's to be used across your app; but with the same inner keys, is to reference the unique ResourceDictionary per element that will use it. This will not be using the App.xaml resources but will be pointing directly to the file location in your application. Since ResourceDictionary's have a default 'Build Action' of 'Page' it will work referencing the location this way. If your ResourceDictionary doesn't work this way the first thing is to check this by right clicking the ResourceDictionary in your solution explorer and make sure that's correct.

Example:

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>

Looks like:

enter image description here

Upvotes: 1

Related Questions