Matthew
Matthew

Reputation: 67

using a Brush stored in a ResourceDictionary

I am a WPF newbie and I want to create a program wide Brush that I can reference again and again. I have a brush in a ResourceDicitonary with full definition.

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:PresentationOptions="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options">
   <DrawingBrush x:Key="rg1024_metal_effect" Stretch="Uniform">

I have added this to app.xaml.

 <Color x:Key="SteelBrush">#FFFFFF</Color>
 <SolidColorBrush x:Key="AppBrush" Color="{StaticResource rg1024_metal_effect}"/>

But I get the error:

"rg1024_metal_effect could not be resolved"

I know this should be easy but for a freshie like me - that's not so easy.

Upvotes: 0

Views: 1529

Answers (2)

Pavel Anikhouski
Pavel Anikhouski

Reputation: 23208

If you want to use a Color in AppBrush, you'll need to define it like <SolidColorBrush x:Key="AppBrush" Color="{StaticResource SteelBrush}"/>

If you would like to add this brush to button, you can use the same way, like <Button Background="{StaticResource SteelBrush}"/> or create s Style for Button in your resource dictionary

<Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}" x:Key="StyleKey">
    <Setter Property="Background" Value="{StaticResource SteelBrush}"/>
…
</Style>

Upvotes: 1

mm8
mm8

Reputation: 169150

If you define a resource in a ResourceDictionary named for example "Dictionary1.xaml" and you want to reference this resource from another resource that you define in another ResourceDictionary, such as for example App.xaml, you should merge Dictionary1.xaml into App.xaml:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Dictionary1.xaml" />
        </ResourceDictionary.MergedDictionaries>
        <SolidColorBrush x:Key="AppBrush" Color="{StaticResource SteelBrush}"/>
    </ResourceDictionary>
</Application.Resources>

Dictionary1.xaml:

<Color x:Key="SteelBrush">#FFFFFF</Color>

In your example, rg1024_metal_effect is not a Color but a DrawingBrush though. You can't set the Color property of a SolidColorBrush to a DrawingBrush.

Upvotes: 2

Related Questions