title: WPF Bind Visibility of Validation Adorner in MVVM
body: <p>I am currently working on an MVVM Solution, using WPF Validation. </p>
  
  <p>What I'd like to do is be able to control when the Validation Adorners are shown using a "ShowErrors" Visibility Property in my Context.</p>
  
  <p>I have the following Template for the WPF ComboBox Validation Adorners, contained within my Application.xaml file;</p>
  
  <pre><code>&lt;Style TargetType="{x:Type ComboBox}"&gt;
              &lt;Setter Property="VerticalAlignment" Value="Center" /&gt;
              &lt;Setter Property="Margin" Value="0,2,40,2" /&gt;
              &lt;Setter Property="Validation.ErrorTemplate"&gt;
                  &lt;Setter.Value&gt;
                      &lt;ControlTemplate&gt;
                          &lt;DockPanel LastChildFill="true"&gt;
                              &lt;Border Background="Red" DockPanel.Dock="right" Margin="5,0,0,0" Width="20" Height="20" CornerRadius="10"
                              ToolTip="{Binding ElementName=customAdorner1, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"&gt;
                                  &lt;TextBlock Text="!" VerticalAlignment="center" HorizontalAlignment="center" FontWeight="Bold" Foreground="white"&gt;
                                  &lt;/TextBlock&gt;
                              &lt;/Border&gt;
                              &lt;AdornedElementPlaceholder Name="customAdorner1" VerticalAlignment="Center" &gt;
                                  &lt;Border BorderBrush="red" BorderThickness="1" /&gt;
                              &lt;/AdornedElementPlaceholder&gt;
                          &lt;/DockPanel&gt;
                      &lt;/ControlTemplate&gt;
                  &lt;/Setter.Value&gt;
              &lt;/Setter&gt;
          &lt;/Style&gt;
  </code></pre>
  
  <p>(I have a separate Template for TextBoxes) </p>
  
  <p>After doing some searching on StackOverflow and Google, I tried adding the following to the DockPanel;</p>
  
  <pre><code>Visibility="{Binding DataContext.ShowErrors, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Mode=TwoWay}"
  </code></pre>
  
  <p>But this doesn't seem to work for me, even though the same method works fine from within the Main XAML.... Any ideas?</p>
  
  <p><strong>EDIT</strong>: My UserControl to which I bind my DataContext to has an x:Name of "MainContext" </p>
  
  <p>I found acouple of theads which suggested;</p>
  
  <pre><code>Visibility="{Binding DataContext.ShowErrors, Source={x:Reference Name=MainContext}, Mode=TwoWay}"&gt;
  </code></pre>
  
  <p>Which gives an error of </p>
  
  <blockquote>
    <p>Unresolved reference 'MainContext'</p>
  </blockquote>
  
  <p>Aswell as;</p>
  
  <pre><code>Visibility="{Binding DataContext.ShowErrors, ElementName=MainContext, Mode=TwoWay}"&gt;
  </code></pre>
  
  <p>Which just doesn't work.</p>
  
  <p><strong>Edit2</strong>: If I move the whole adorner out of the Appliation.xaml and into the UserControl Resources, then use;</p>
  
  <pre><code>Visibility="{Binding DataContext.ShowErrors, ElementName=MainContext, Mode=TwoWay}"&gt;
  </code></pre>
  
  <p>It works... Not ideal though, as I don't want to have to repeat the Template across all my Screens</p>
  
  <p><strong>Edit 3:</strong> Ok, so I've found a workaround for now. I used the following for the Visibility Binding...</p>
  
  <pre><code>Visibility="{Binding ElementName=customAdorner1, Path=AdornedElement.Parent.DataContext.ShowErrors, Converter={StaticResource MyBolVisibilityConverter}, Mode=TwoWay}"
  </code></pre>
  
  <p>I then added a Boolean ShowErrors Property to the context and added a Converter to convert from the Boolean Value to a Visibility Value, mainly because of where the ShowErrors Property actually ended up.</p>
  
  <p>This was slightly more confusing as my Form is a Master Details arrangement, where the Adorners are shown within the details section, which has it's own DataContext. This, of course, doesn't have access to the UserControl's DataContext directly.</p>
  
  <p>This seems like a bit of a hack to me really, so I would appreciate a better solution!</p>