title: WPF. How to make a binding into Brush to property of ControlTemplate?
body: <p>Good afternoon! </p>
  
  <p>I made control. There is it code:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class ShutterWidget : Control
  { // this Control changes own Background property (changes brush) in some time }
  </code></pre>
  
  <p>And ResourceDictionary with style for it:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                  xmlns:widgets="clr-namespace:ProcessControl.Widgets"&gt;
  
  &lt;DrawingBrush x:Key="ShutterBrush" x:Shared ="true"  Stretch="Uniform"&gt;
      &lt;DrawingBrush.Drawing&gt;
          &lt;DrawingGroup&gt;
              &lt;GeometryDrawing Brush="{Binding Background, RelativeSource={RelativeSource TemplatedParent}}" Geometry="F1M84.7698,0.8018L84.7698,36.1568 106.761,14.2929 106.761,0.8018 84.7698,0.8018z"/&gt;
              &lt;GeometryDrawing Geometry="F1M84.7698,0.8018L84.7698,36.1568 106.761,14.2929 106.761,0.8018 84.7698,0.8018z"&gt;
                  &lt;GeometryDrawing.Pen&gt;
                      &lt;Pen Brush="Black" EndLineCap="Round" LineJoin="Round" StartLineCap="Round" Thickness="1.6036"/&gt;
                  &lt;/GeometryDrawing.Pen&gt;
              &lt;/GeometryDrawing&gt;
          &lt;/DrawingGroup&gt;
      &lt;/DrawingBrush.Drawing&gt;
  &lt;/DrawingBrush&gt;
  
  &lt;Style TargetType="widgets:ShutterWidget"&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="widgets:ShutterWidget"&gt;
                  &lt;Rectangle Fill="{StaticResource ShutterBrush}"/&gt;    
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p></p>
  
  <p>This dictionary include template for display this Control. This template uses DrawingBrush.</p>
  
  <p>I want to change of fill of the parts of DrawingBrush by changing the background property of ShutterWidget.</p>
  
  <p>UPDATE: </p>
  
  <p>I went another way, because it seems to me to change parts of DrawingBrush difficult. I decided change whole DrawingBrush on another. But now I got one more problem. 
  I created different ResourceDictionary and each includes one DrawingBrush. From code behind I load them using XamlReader.Load. But XamlReader throw exception " Failed to create "ImageSource" text-based "valve_off_files/image0.png". Directory "valve_off_files" includes extra png files( I get ResourceDictionary from emf file using ExpressionDesign. It creates xaml file and extra directory with extra image file.). And when I load XamlReader doesn't find extra file image0.png which is used in DrawingBrush.
  Why doesn't XamlReader see this file image0.png?</p>
  
  <p>Below is a part of code from ResourceDictionary wich was created ExpressionDesign.</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
      &lt;DrawingBrush x:Key="valve_off" Stretch="Uniform"&gt;
          &lt;DrawingBrush.Drawing&gt;
              &lt;DrawingGroup&gt;
                  &lt;DrawingGroup.Children&gt;
                      &lt;ImageDrawing ImageSource="valve_off_files/image0.png" Rect="0,0,88,96"/&gt;
                  &lt;/DrawingGroup.Children&gt;
  </code></pre>
  
  <p>And so I load xaml files:</p>
  
  <pre class="lang-cs prettyprint-override"><code>    public static Brush GetItem(string name)
      {
          using (var str = new FileStream($"Resources/{name}.xaml", FileMode.Open))
          {
              var dct = (ResourceDictionary)XamlReader.Load(str);
              return (Brush)dct?[name];
          }
      }
  </code></pre>
  
  <p>UPDATE 2:</p>
  
  <p>I found a solution. I was helped using of other method of XamlReader which accept ParserContext. XamlReader.Load(FileStream, ParserContext). After I set BaseUri property of ParserContext, XamlReader has read xaml file correctly. </p>
  
  <p>Thanks a lot all people who tried to help me.</p>