How to define and use resources in xaml so they can be used in C#

Theoretically, I think that I can define Brushes and Colors etc. in an xaml file and assign that to a button.background in c#. But how do I do that? Where do I put my lineargradientbrush definition like this:

<LinearGradientBrush x:Key="BlaBrush">
                <GradientStop Offset="0" Color="Red"/>
                <GradientStop Offset="1" Color="Green"/>

Just putting it at various places in my window’s xaml file results in various error messages :/

I found this question here on stackoverflow: which explains a part of it, but he seems to know where to do the Brush definition.

I also tried adding the shinyblue.xaml wpf template to the app and added <ResourceDictionary Source=”ShinyBlue.xaml”/> to the application.resources in app.xaml. This makes all my buttons blue instantly, but still, the “things” defined in shinyblue.xaml like NormalBrush is not accessible from C# – at least I don’t know how.


In WPF XAML how can I concatenate 2 constants so I can use pre-define paths?

I’m new to XAML, so presume I’m missing something simple. I want to replace the path part of the source path with a c# constant for easier path management, for example I have: <Image Source=/Image

How can I define and use a variable in XAML to define a color?

I have a number of elements in my XAML which define Foreground=#555 to be the color of faded text in a readonly textbox. <TextBlock Text={Binding SingularModelClassFileTitle} Margin=0 10 0 0

How can I define a variable in XAML?

I have the following two buttons in XAML: <Button Content=Previous Margin=10,0,0,10/> <Button Content=Next Margin=0,0,10,10/> How can I define 10 to be a variable so I can chang

How can a method in C# be used in XAML?

I have a method creating a cone, such as in the following code snippet. Could you explain me how this cone created by CreateCone(…) can be used in a XAML file in the same solution. C# Code snippet :

How can I convert or translate C++ define macros so I can use them in C#

I have the following macros I’m trying to convert. #define FIELD_OFFSET(type, field) ((LONG)&(((type *)0)->field)) #define IMAGE_FIRST_SECTION(ntheader) ((PIMAGE_SECTION_HEADER) ((DWORD)ntheade

How can I use encrypted xml file as application resources for xaml data binding?

I want to use encrypted xml file as application resource for easy xaml data binding. Xml file are encrypted in different application. I can use unencrypted xml for data binding. I cant use the same me

How to use Popup defined in App.xaml Resources?

I have a popup defined inside App.xaml resources: <Application.Resources> <Popup x:Key=popup> //some content here </Popup> </Application.Resources> I want to use it in this

How to define a variable that can be used everywhere?

How do I define a variable that I can use everywhere in my program in C#.

Can I define a variable in XAML?

So, this used to be possible in WPF & SL: <Grid.Resources> <x:Double x:Name=MyDouble>1.2</x:Double> </Grid.Resources> But in WinRT-XAML it just underlines with: TypeCon

Can I use enums in Java to define what class to call/use? If so, how?

Is it possible to use enums in Java to define what class I need to call at runtime? For example, if I have 3 enums and 3 classes, all using the same interface/common method names, can I evaluate a str


Put them in the Resources collection of one of your elements in XAML:

<Window ...>
        <LinearGradientBrush x:Key="BlaBrush">
            <GradientStop Offset="0" Color="Red"/>
            <GradientStop Offset="1" Color="Green"/>
        <!-- Other resources -->
    <!-- Contents of window -->

Then get them in code by using FindResource

var blaBrush = this.FindResource("BlaBrush") as LinearGradientBrush;

See Resources Overview for more information.

Your xaml would look something like this:


<Window x:Class="BrushResource.MainWindow"
    Title="MainWindow" Height="350" Width="525">

    <LinearGradientBrush x:Key="BrushOne" StartPoint="0,0.5" EndPoint="1,0.5" Opacity="0.5">
                <GradientStop Color="Black" Offset="0" />
                <GradientStop Color="Silver" Offset="1" />

    <LinearGradientBrush x:Key="BrushTwo" StartPoint="0,0.5" EndPoint="1,0.5" Opacity="0.5">
                <GradientStop Color="Maroon" Offset="0" />
                <GradientStop Color="Silver" Offset="1" />

    <Button Content="Button" Width="100" Click="myButton_Click"/>

To assign the value, you need to grab the gradient brush from the resources like this:


private void myButton_Click(object sender, RoutedEventArgs e)
        (sender as Button).Background = this.Resources["BrushOne"] as LinearGradientBrush;

You can access the application resources as

Application.Current.Resources["BlaBrush"] as LinearGradientBrush

Or, you add the resource to the control’s resources and access them like Quartermeister wrote.

Note that the existing answers talk about putting the resources in Window.Resources. If you want the resources to be available application-wide, you might consider putting them in App.xaml or better yet, create stand-alone resource dictionaries that can be included in your views and re-used elsewhere (including other projects)

            <ResourceDictionary Source="DefaultStyles.xaml"/>
        <Style x:Key="my_style" />