title: Can you access the gradient colour created in XAML in c# wpf
body: <p>Similar questions may exists, but none of them seems to be helpful. So I'll try to explain a more specific case, and see if anyone can help me.</p>
  
  <p>I have an application that has a label with a gradient background. I created the gradient background using XAML, my program changes the background fine, but I want the background to change back to "normal" after the user clicks the reset button. How can I do this as still new to wpf? The code in XAML <strong>"LinearGradientBrush x:Key="headerBackground"</strong> is what I want to access in code behind</p>
  
  <pre><code>&lt;Window
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:syncfusion="http://schemas.syncfusion.com/wpf" x:Class="ItemWrapPanel.MainWindow"
      Title="MainWindow" Height="350" Width="600"&gt;
  &lt;Window.Resources&gt;
      **&lt;LinearGradientBrush x:Key="headerBackground" StartPoint="0,0" EndPoint="0,1"&gt;
          &lt;GradientStop Color="#FFEFEEEE" Offset="0"/&gt;
          &lt;GradientStop Color="#E7E7E7E7" Offset="1"/&gt;
      &lt;/LinearGradientBrush&gt;**
      &lt;LinearGradientBrush x:Key="selectedHeaderBackground" StartPoint="0,0" EndPoint="0,1"&gt;
          &lt;GradientStop Color="#F6CD1D" Offset="0"/&gt;
          &lt;GradientStop Color="#EBA32A" Offset="1"/&gt;
      &lt;/LinearGradientBrush&gt;
  &lt;/Window.Resources&gt;
  &lt;Grid x:Name="MainGrid"&gt;
      &lt;Grid.RowDefinitions&gt;
          &lt;RowDefinition x:Name="Row1"/&gt;
      &lt;/Grid.RowDefinitions&gt;
      &lt;Grid.ColumnDefinitions&gt;
          &lt;ColumnDefinition x:Name="Col1"/&gt;
          &lt;ColumnDefinition x:Name="Col2"/&gt;
          &lt;ColumnDefinition x:Name="Col3"/&gt;
      &lt;/Grid.ColumnDefinitions&gt;
      &lt;Label x:Name="Label1"
          Content="Rock Properties"
          Grid.Row="0"
          Grid.Column="0"
          MouseDoubleClick="Label1_MouseDoubleClick"
          Background="{DynamicResource headerBackground}"
          VerticalAlignment="Top"/&gt;
      &lt;Label x:Name="Label2"
          Content="Contacts"
          Grid.Column="1"
          Grid.Row="0"
          Background="{DynamicResource headerBackground}"
          MouseDoubleClick="Label2_MouseDoubleClick" 
          Margin="0,0,0,282"
          VerticalAlignment="Top"/&gt;
      &lt;Label x:Name="Label3"
          Content="Fluid Properties"
          Grid.Column="2"
          Background="{DynamicResource headerBackground}"
          Grid.Row="0"
          MouseDoubleClick="Label3_MouseDoubleClick" 
          Margin="0,0,0,282"
          VerticalAlignment="Top"/&gt;
      &lt;Button x:Name="Resetbtn" Content="Reset" HorizontalAlignment="Left" Margin="0,70,0,0" VerticalAlignment="Top" Width="75" Click="Reset_Click" Background="{DynamicResource selectedHeaderBackground}"/&gt;
      &lt;Button Name="CloseRock" Content="X" HorizontalAlignment="Left" Margin="173,4,0,0" VerticalAlignment="Top" Width="24" Grid.Column="0"/&gt;
      &lt;Button Name="CloseContacts" Content="X" HorizontalAlignment="Left" Margin="174,2,0,0" VerticalAlignment="Top" Width="24" Grid.Column="1"/&gt;
      &lt;Button Name="CloseFluid" Content="X" HorizontalAlignment="Left" Margin="173,2,0,0" VerticalAlignment="Top" Width="24" Grid.Column="2"/&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>C#</strong></p>
  
  <pre><code> private ColumnDefinition SelectedColumn;
      private Color selectedHeaderBackground = (Color)ColorConverter.ConvertFromString("#EBA32A");
  
      public MainWindow()
      {
          InitializeComponent();
      }
  
      private void Label1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
      {
          SelectedColumn = Col1;
          SetCol1Header();
          SetColumnWidth();
          SetColumnWidthCol1();
      }
  
      private void Label2_MouseDoubleClick(object sender, MouseButtonEventArgs e)
      {
          SelectedColumn = Col2;
          SetCol2Header();
          SetColumnWidth();
          SetColumnWidthCol2();
      }
  
      private void Label3_MouseDoubleClick(object sender, MouseButtonEventArgs e)
      {
          SelectedColumn = Col3;
          SetCol3Header();
          SetColumnWidth();
          SetColumnWidthCol3();
      }
  
      public void SetColumnWidth()
      {
          SelectedColumn.Width = new GridLength(1, GridUnitType.Star);
      }
  
      private void Reset_Click(object sender, RoutedEventArgs e)
      {
          ResetHeaderBackground();
          ResetColumnWidth();
          ResetColumns();
      }
  
      private void ResetColumns()
      {
          Label1.Visibility = System.Windows.Visibility.Visible;
      }
  
      private void ResetColumnWidth()
      {
          Col1.Width = new GridLength(1, GridUnitType.Star);
          Col2.Width = new GridLength(1, GridUnitType.Star);
          Col3.Width = new GridLength(1, GridUnitType.Star);
      }
  
      private void ResetHeaderBackground()
      {
          //Label3.Background = new SolidColorBrush(HeaderBackground);
          //Label2.Background = new SolidColorBrush(HeaderBackground);
          //Label1.Background = new SolidColorBrush(HeaderBackground);
      }
  
      private void MinimiseRock_Click(object sender, RoutedEventArgs e)
      {
          MinimiseWindow();
      }
  
      private void MinimiseContacts_Click(object sender, RoutedEventArgs e)
      {
          MinimiseWindow();
      }
  
      private void MinimiseFluid_Click(object sender, RoutedEventArgs e)
      {
          MinimiseWindow();
      }
  
      private void MinimiseWindow()
      {
          this.WindowState = WindowState.Minimized;
      }
  
      private void SetCol1Header()
      {
          Label1.Background = new SolidColorBrush(selectedHeaderBackground);
      }
  
      private void SetCol2Header()
      {
          Label2.Background = new SolidColorBrush(selectedHeaderBackground);
      }
  
      private void SetCol3Header()
      {
          Label3.Background = new SolidColorBrush(selectedHeaderBackground);
      }
  
      private void SetColumnWidthCol1()
      {
          Col2.Width = new GridLength(150);
          Col3.Width = new GridLength(150);
      }
  
      private void SetColumnWidthCol2()
      {
          Col1.Width = new GridLength(150);
          Col3.Width = new GridLength(150);
      }
  
      private void SetColumnWidthCol3()
      {
          Col1.Width = new GridLength(150);
          Col2.Width = new GridLength(150);
      }
  
  }
  </code></pre>