title: Data binding outside the visual tree. Data Context bridging
body: <p>I am trying to bind a dynamic behavior to a visual element outside the WPF logical and visual trees. </p>
  
  <p>My problem is that the RadChart plot color is given in (quasi path):
  RadChart.SeriesMapping.LineSeriesDefinition.Appearance.Stroke</p>
  
  <p>I originally wanted to Bind that to a property of the chart datacontext in XAML. Naively, I just wrote a regular {Binding PlotBrush}</p>
  
  <p>Compiler returned "Cannot Find Governing FrameWorkelement" error. After reading up, I reckon that this means that resolving the datacontext up the hierarchy did not work. Because its ancestors (XAML speaking) has other types than FrameWorkElement and other relationships than being contents of a contents control. 
  At least, that is my current understanding of it. Please correct me.</p>
  
  <p>So, I found the "DataContext Bridge" 
  <a href="http://www.codeproject.com/KB/WPF/AttachingVirtualBranches.aspx" rel="noreferrer">http://www.codeproject.com/KB/WPF/AttachingVirtualBranches.aspx</a></p>
  
  <p>Simply speaking it says that you <strong>bind</strong> the datacontext property of the framework element that is at runtime assigned the datacontext (not any of those that inherits it) <strong>to</strong> the datacontext of a <strong>FrameWorkElement instance within the resources</strong>. Then the same resource object instance is used to bind to the datacontext property of a "branch" that you wish to "attach" to the DataContext inheritance dynamic. But the author of the article had the luxury of being able to implement the validationrule consumer of the observed property. SolidColorBrush is sealed and I imagine it would be quite some work to implement a complete Brush, even using a decorator.</p>
  
  <p>In my case, this does not help me do what I want, but I am "so close". So I wonder if there is some aspect of XAML tricks that could help me out.</p>
  
  <pre><code>&lt;Window.Resources&gt;
      &lt;local:FrameWorkElement x:Key="DataContextBridge"/&gt;
  &lt;/Window.Resources&gt;
  </code></pre>
  
  <p>However, it is unclear how I utilize this. There is no object whose datacontext should be set. AppearanceSettings is not a FrameWorkElement.</p>
  
  <pre><code>&lt;telerik:SeriesAppearanceSettings&gt;
     &lt;telerik:SeriesAppearanceSettings.Stroke&gt;
         Ok, how do I use the fact that I can access the datacontext here?                                         
     &lt;/telerik:SeriesAppearanceSettings.Stroke&gt;
  &lt;/telerik:SeriesAppearanceSettings&gt;
  </code></pre>
  
  <p>So, the next step was wether I could get the brush object directly somehow. I experimented with this kind of thing, just messing around:</p>
  
  <p>.cs :</p>
  
  <pre><code>public class ObservableBrush : FrameworkElement
  {
      public Brush Brush
      {
          get { return (Brush) GetValue(BrushProperty); }
          set { SetValue(BrushProperty, value); }
      }
  
      public static readonly DependencyProperty BrushProperty =
          DependencyProperty.Register("Brush", typeof (Brush), typeof (ObservableBrush), new UIPropertyMetadata(new SolidColorBrush(Colors.Black)));
  }
  </code></pre>
  
  <p>Top of XAML:</p>
  
  <pre><code>&lt;Window.Resources&gt;
      &lt;local:ObservableBrush x:Key="StrokeBrush"/&gt;
  &lt;/Window.Resources&gt;
  </code></pre>
  
  <p>Inline XAML:</p>
  
  <pre><code>&lt;telerik:SeriesAppearanceSettings.Stroke&gt;
       &lt;Binding Path="Brush"&gt;
       &lt;Binding.Source&gt;
            &lt;Binding Source="{StaticResource ResourceKey=StrokeBrush}" Path="DataContext"/&gt;
       &lt;/Binding.Source&gt;                                            
       &lt;/Binding&gt;                                     
  &lt;/telerik:SeriesAppearanceSettings.Stroke&gt;
  </code></pre>
  
  <p>"Binding" is not a frameworkelement, nor is "Source" a dependencyproperty, either, so the runtime of course complains. I am aware that the Brush property wont ever return anything other than the default value given in the dependency property registration.</p>
  
  <p>I am sort of going on the second day straight on this problem. I think that my next two attempts will be to:
  * Make ObservableBrush an actual Brush. Then programatically set it (effectively using standard dynamic resource binding instead). I don't like it. I wanted to make databinding work.
  * Bridge a BRUSH instead of DATACONTEXT. </p>
  
  <p>XAML part of this works fine:</p>
  
  <pre><code>&lt;telerik:SeriesAppearanceSettings.Stroke&gt;
       &lt;Binding Source="{StaticResource ResourceKey=StrokeBrush}" Path="Brush"/&gt;
  &lt;/telerik:SeriesAppearanceSettings.Stroke&gt;
  </code></pre>
  
  <p>But again, how do I bind the Brush to the DataContext property? Is there some override that I can use within ObservableBrush to make Brush dynamically follow the one in the datacontext?</p>
  
  <p>How about creating a fake visual element within the tree and then associate TWO bindings to it? </p>
  
  <pre><code>&lt;!-- Within the visual tree scope --&gt;
  &lt;SomeFrameWorkElementType&gt;
       &lt;SomeFrameWorkElemetType.SomeBrushProp&gt;
           &lt;Binding Source="{StaticResource ResourceKey=StrokeBrush}" Path="Brush" Mode="OneWayToSource"/&gt;
           &lt;Binding Stroke/&gt;
       &lt;/SomeFrameWorkElemetType.SomeBrushProp&gt;
  &lt;SomeFrameWorkElementType&gt;
  </code></pre>
  
  <p>And this somehow will "connect" the two bindings? </p>
  
  <p>Or is there some (un)official "helper class" for this type of functionality?</p>
  
  <p>Or am I barking up the wrong tree, and (much) better off solving this in the code-behind through dynamic resource binding?</p>
  
  <p>Any thoughts or observations on how to go on about this? Other than on my apparent self destructiveness for insisting on databinding when dynamic resources should be solving this.</p>