Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"/>
</LinearGradientBrush>

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

I found this question here on stackoverflow: How to use a defined brush resource in XAML, from C# 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.

Marc

like image 453
marc40000 Avatar asked Jul 22 '10 12:07

marc40000


People also ask

How do I add resources to XAML dictionary?

Tip You can create a resource dictionary file in Microsoft Visual Studio by using the Add > New Item… > Resource Dictionary option from the Project menu. Here, you define a resource dictionary in a separate XAML file called Dictionary1.

What is resources in XAML?

A resource is an object that can be reused in different places in your app. Examples of resources include brushes and styles. This overview describes how to use resources in Extensible Application Markup Language (XAML). You can also create and access resources by using code.

Is XAML a programming language?

XAML is a new descriptive programming language developed by Microsoft to write user interfaces for next-generation managed applications. XAML is the language to build user interfaces for Windows and Mobile applications that use Windows Presentation Foundation (WPF), UWP, and Xamarin Forms.


2 Answers

Your xaml would look something like this:

MainWindow.xaml

<Window x:Class="BrushResource.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">

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

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

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

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

MainWindow.xaml.cs

private void myButton_Click(object sender, RoutedEventArgs e)
    {
        (sender as Button).Background = this.Resources["BrushOne"] as LinearGradientBrush;
    }
like image 197
JSprang Avatar answered Oct 18 '22 15:10

JSprang


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)

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="DefaultStyles.xaml"/>
        </ResourceDictionary.MergedDictionaries>
        <Style x:Key="my_style" />
    </ResourceDictionary>
</UserControl.Resources>
like image 43
Brian Genisio Avatar answered Oct 18 '22 14:10

Brian Genisio