Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
894 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...