Accessing XAML User Resources in Code behind C#

<UserControl x: Class="MyApp.ResourceHierarchy" 
 xmlns="http: //schemas. microsoft. com/winfx/2006/xaml/presentation"  
 xmlns: x="http: //schemas. microsoft. com/winfx/2006/xaml"  
 Width="400" Height=" 300"> 
  <Grid x: Name="LayoutRoot" Background="White"> 
    <StackPanel> 
      <StackPanel.Resources> 
        <LinearGradientBrush x: Key="ButtonStylee"> 
          <GradientStop Offset="0.00" Color=" Yellow" /> 
          <GradientStop Offset="0.50" Color="White" /> 
          <GradientStop Offset="1.00" Color="Purple" />
       </LinearGradientBrush> 
      </StackPanel. Resources> 
      <Button Content=" Click Me First" Margin=" 5" 
      Name="btn_StyleMe"></Button> 
    </StackPanel> 
  </Grid> 
</UserControl>
LinearGradientBrush brush = (LinearGradientBrush) this.Resources["ButtonStylee"] ; 
 
// Swap the color order. 
Color color = brush.GradientStops[0]. Color; 
brush.GradientStops[0]. Color = brush. GradientStops[ 2] .Color; 
brush.GradientStops[2]. Color = color;
 
btn_StyleMe.foreground = brush;

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.