title: WPF binding issues with resource dictionaries
body: <p>I have developed a custom data grid control.  This control has two parts, one is the custom template used by the data grid itself and the other is my custom control which adds a title area to the data grid.</p>
  
  <p>I have a class called CustomDataGrid, here is the XAML.</p>
  
  <pre><code>&lt;UserControl x:Class="MDT_Designer.Presentation.ScreenControls.CustomDataGrid"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
           xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
           mc:Ignorable="d" 
           Name="CDataGrid" &gt;
  &lt;UserControl.Resources&gt;
      &lt;ResourceDictionary x:Key="Dictionary" Source="CustomizedControls.xaml"/&gt;
  &lt;/UserControl.Resources&gt;
  &lt;Grid&gt;
      &lt;Grid.RowDefinitions&gt;
          &lt;RowDefinition Height="{Binding ElementName=CDataGrid, Path=TitleAreaHeight}"/&gt;
          &lt;RowDefinition Height="{Binding ElementName=CDataGrid, Path=GridAreaHeight}"/&gt;
      &lt;/Grid.RowDefinitions&gt;
      &lt;Grid Name="TitleArea" DockPanel.Dock="Top" Background="{Binding ElementName=CDataGrid, Path=TitleBackColor}" &gt;
          &lt;Viewbox HorizontalAlignment="{Binding ElementName=CDataGrid, Path=TitleAlignment}"&gt;
              &lt;TextBlock Name="TitleText" Text="{Binding ElementName=CDataGrid, Path=Title}" Margin="2,0,0,0" FontFamily="{Binding ElementName=CDataGrid, Path=TitleFontFamily}" Foreground="{Binding ElementName=CDataGrid, Path=TitleTextColor}"/&gt;
          &lt;/Viewbox&gt;
      &lt;/Grid&gt;
      &lt;DataGrid Name="DataGridArea" Grid.Row="1" IsReadOnly="True" HorizontalGridLinesBrush="DarkGray" VerticalGridLinesBrush="DarkGray" VerticalScrollBarVisibility="Visible" Background="AliceBlue" &gt;
      &lt;/DataGrid&gt;
  &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>In my CustomDataGrid.xaml.cs file I have defined properties as such.</p>
  
  <pre><code>    public static DependencyProperty TitleAlignmentProperty = DependencyProperty.Register("TitleAlignment", typeof(HorizontalAlignment), typeof(CustomDataGrid));
      public HorizontalAlignment TitleAlignment
      {
          get { return (HorizontalAlignment)base.GetValue(TitleAlignmentProperty); }
          set { base.SetValue(TitleAlignmentProperty, value); }
      }
  </code></pre>
  
  <p>I am just showing one as a sample, and this works.  I do wish that I could change the binding in the XAML to get rid of ElementName=CDataGrid, but any way I have as an alternative doesn't work.</p>
  
  <p>Now to my problem.  My custom template for the data grid control itself is stored in the dictionary CustomizedControls.xaml.</p>
  
  <p>Here is one example of binding issues.  I had to create this resource.</p>
  
  <pre><code>&lt;LinearGradientBrush x:Key="HeaderBackgroundBrush" EndPoint="0.5,1" StartPoint="0.5,0" &gt;
      &lt;GradientStop Color="{DynamicResource {x:Static SystemColors.ControlLightColorKey}}" Offset="0" /&gt;
      &lt;GradientStop Color="{DynamicResource {x:Static SystemColors.ControlDarkColorKey}}" Offset="1" /&gt;
  &lt;/LinearGradientBrush&gt;
  </code></pre>
  
  <p>Then I use it in Border definition for the column header.</p>
  
  <pre><code>&lt;Style TargetType="{x:Type DataGridColumnHeader}"&gt;
      &lt;Setter Property="VerticalContentAlignment" Value="Center" /&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type DataGridColumnHeader}"&gt;
                  &lt;Grid&gt;
                      &lt;Border x:Name="columnHeaderBorder" BorderThickness="1" Padding="3,0,3,0" 
              Background="{DynamicResource HeaderBackgroundBrush}"&gt; 
                              &lt;Border.BorderBrush&gt;
                              &lt;LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"&gt;
                                  &lt;GradientStop Color="{DynamicResource BorderLightColor}" Offset="0" /&gt;
                                  &lt;GradientStop Color="{DynamicResource BorderDarkColor}" Offset="1" /&gt;
                              &lt;/LinearGradientBrush&gt;
                          &lt;/Border.BorderBrush&gt;
                          &lt;ContentPresenter x:Name="columnHeaderPresenter"  HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Style="{DynamicResource HeaderContentStyle}" /&gt;
                      &lt;/Border&gt;
  
                      &lt;Thumb x:Name="PART_LeftHeaderGripper" HorizontalAlignment="Left" Style="{StaticResource ColumnHeaderGripperStyle}" /&gt;
                      &lt;Thumb x:Name="PART_RightHeaderGripper" HorizontalAlignment="Right" Style="{StaticResource ColumnHeaderGripperStyle}" /&gt;
                  &lt;/Grid&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p>Then to change it in my code behind I have to do this.</p>
  
  <pre><code>        LinearGradientBrush headerBrush = (LinearGradientBrush)TryFindResource("HeaderBackgroundBrush");
          headerBrush.GradientStops[0] = new GradientStop(brush.Color, 0.0);
  </code></pre>
  
  <p>I cannot change the brush itself, I can just change a property of the brush.  I have tried every binding technique I can think of, but nothing seems to work when the style is in a dictionary.</p>
  
  <p>What I would like to be able to do is somehow bind something like BorderLightColor, which is BorderBrush setting, so that in my code behind I can change it's value.</p>
  
  <p>Any help would make my day, it would make my whole weekend in fact.  Thanks.</p>