I am currently working on an MVVM Solution, using WPF Validation.
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.
I have the following Template for the WPF ComboBox Validation Adorners, contained within my Application.xaml file;
<Style TargetType="{x:Type ComboBox}">
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Margin" Value="0,2,40,2" />
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel LastChildFill="true">
<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}">
<TextBlock Text="!" VerticalAlignment="center" HorizontalAlignment="center" FontWeight="Bold" Foreground="white">
</TextBlock>
</Border>
<AdornedElementPlaceholder Name="customAdorner1" VerticalAlignment="Center" >
<Border BorderBrush="red" BorderThickness="1" />
</AdornedElementPlaceholder>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
(I have a separate Template for TextBoxes)
After doing some searching on StackOverflow and Google, I tried adding the following to the DockPanel;
Visibility="{Binding DataContext.ShowErrors, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Mode=TwoWay}"
But this doesn't seem to work for me, even though the same method works fine from within the Main XAML.... Any ideas?
EDIT: My UserControl to which I bind my DataContext to has an x:Name of "MainContext"
I found acouple of theads which suggested;
Visibility="{Binding DataContext.ShowErrors, Source={x:Reference Name=MainContext}, Mode=TwoWay}">
Which gives an error of
Unresolved reference 'MainContext'
Aswell as;
Visibility="{Binding DataContext.ShowErrors, ElementName=MainContext, Mode=TwoWay}">
Which just doesn't work.
Edit2: If I move the whole adorner out of the Appliation.xaml and into the UserControl Resources, then use;
Visibility="{Binding DataContext.ShowErrors, ElementName=MainContext, Mode=TwoWay}">
It works... Not ideal though, as I don't want to have to repeat the Template across all my Screens
Edit 3: Ok, so I've found a workaround for now. I used the following for the Visibility Binding...
Visibility="{Binding ElementName=customAdorner1, Path=AdornedElement.Parent.DataContext.ShowErrors, Converter={StaticResource MyBolVisibilityConverter}, Mode=TwoWay}"
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.
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.
This seems like a bit of a hack to me really, so I would appreciate a better solution!