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

前端 未结 4 1833
时光取名叫无心
时光取名叫无心 2020-12-16 12:35

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 linear

相关标签:
4条回答
  • 2020-12-16 13:05

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

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

    Then get them in code by using FindResource

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

    See Resources Overview for more information.

    0 讨论(0)
  • 2020-12-16 13:24

    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>
    
    0 讨论(0)
  • 2020-12-16 13:28

    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.

    0 讨论(0)
  • 2020-12-16 13:29

    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;
        }
    
    0 讨论(0)
提交回复
热议问题