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

前端 未结 4 1836
时光取名叫无心
时光取名叫无心 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:29

    Your xaml would look something like this:

    MainWindow.xaml

    
    
    
        
            
                
                    
                    
                
            
        
    
        
            
                
                    
                    
                
            
        
    
    
    
        

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

提交回复
热议问题