title: Changing gradient style programatically in WP8
body: <p>I have a few custom controls in my app. One of those custom controls contains a gradient background, and I would like the user to be able to switch between linear and radial gradient style dynamically. How do I do this in code? My custom control XAML:</p>
  
  <pre><code>&lt;UserControl x:Class="App.tile"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  mc:Ignorable="d"
  FontFamily="{StaticResource PhoneFontFamilyNormal}"
  FontSize="{StaticResource PhoneFontSizeNormal}"
  Foreground="{StaticResource PhoneForegroundBrush}" Height="191.94" Width="406.91"&gt;
  
  &lt;Grid x:Name="LayoutRoot"&gt;
      &lt;Grid.Background&gt;
          &lt;RadialGradientBrush&gt;
              &lt;GradientStop Color="#FF092949" Offset="0"/&gt;
              &lt;GradientStop Color="#FF06192C" Offset="1"/&gt;
          &lt;/RadialGradientBrush&gt;
      &lt;/Grid.Background&gt;
      &lt;TextBlock HorizontalAlignment="Left" Margin="10,134,0,0" TextWrapping="Wrap" x:Name="tileTitle" Text="Project" VerticalAlignment="Top" FontSize="36"/&gt;
  &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>Code where I'll change it's appearance:</p>
  
  <pre><code>private void linearRadioButton_Checked(object sender, RoutedEventArgs e)
  {
      // The linearRadioButton was checked, update the exampleTile
  }
  </code></pre>
  
  <p>Thanks a lot!</p>
  
  <p><strong>Update:</strong></p>
  
  <p>Tried this now:</p>
  
  <pre><code>    &lt;phone:PhoneApplicationPage.Resources&gt;
      &lt;LinearGradientBrush x:Key="linearBrush" StartPoint="0,0.5" EndPoint="1,0.5"&gt;
          &lt;GradientStop Color="Yellow" Offset="0.0" /&gt;
          &lt;GradientStop Color="Red" Offset="0.25" /&gt;
          &lt;GradientStop Color="Blue" Offset="0.75" /&gt;
          &lt;GradientStop Color="LimeGreen" Offset="1.0" /&gt;
      &lt;/LinearGradientBrush&gt;
  
      &lt;RadialGradientBrush x:Key="radialBrush" GradientOrigin="0.5,0.5" Center="0.5,0.5" RadiusX="0.5" RadiusY="0.5"&gt;
          &lt;RadialGradientBrush.GradientStops&gt;
              &lt;GradientStop Color="Yellow" Offset="0" /&gt;
              &lt;GradientStop Color="Red" Offset="0.25" /&gt;
              &lt;GradientStop Color="Blue" Offset="0.75" /&gt;
              &lt;GradientStop Color="LimeGreen" Offset="1" /&gt;
          &lt;/RadialGradientBrush.GradientStops&gt;
      &lt;/RadialGradientBrush&gt;
  &lt;/phone:PhoneApplicationPage.Resources&gt;
  
          private void settingsPage_Loaded(object sender, RoutedEventArgs e)
      {
          // settingsPage was loaded, set the attributes for the exampleTile
          exampleTile.tileTitle.Text = "History";
  
          // Get the gradient type from settings
          try
          {
              gradientType = settings["gradientType"].ToString();
          }
          catch (KeyNotFoundException)
          {
              gradientType = "radial";
          }
  
          if (gradientType.Equals("radial"))
          {
              // Check the radialRadioButton
              radialRadioButton.IsChecked = true;
          }
          else if (gradientType.Equals("linear"))
          {
              // Check the linearRadioButton
              linearRadioButton.IsChecked = true;
          }
      }
  
          private void linearRadioButton_Checked(object sender, RoutedEventArgs e)
      {
          // The linearRadioButton was checked, update the exampleTile
          (exampleTile as tile).Background = (LinearGradientBrush)this.Resources["linearBrush"];
      }
  
      private void radialRadioButton_Checked(object sender, RoutedEventArgs e)
      {
          // The radialRadioButton was checked, update the exampleTile
          this.exampleTile.Background = (LinearGradientBrush)this.Resources["radialBrush"];
      }
  </code></pre>
  
  <p>But it causes a NullReferenceException (Not set to an instance of an object)</p>