Reputation: 9326
In our project we have a LookUp Base ViewModel
which has a Required-Attribute
on the SelectedItem
property. I have another View that uses one of the child LookUps
, which should ignore the Required
-attribute. I've already overridden the IsValid
method in the ViewModel, so saving without caring about the Required
works correctly, but unfortunately it's still showing the validation error on the view when I empty the LookUp
:
I had a couple of possibilities:
Split the LookUpBaseViewModel
into two childs that are also "BaseViewModels", one with the RequiredAttribute
and one without. This works, but it seems like a bit too much work and a lot of extra classes for just a single view that doesn't need to show the validation errors on the View.
Replace the RequiredAttribute
with a RequiredIf-Attribute
and add a boolean IsRequired
parameter to the Constructor. This doesn't work since we use AutoFac
in our project, so we can't use a boolean parameter for the ILookUpBaseViewModel
-interface implementation.
Add a style to the LookUp-ContentControl
in the View to hide the ValidationError border & text
. This seemed like the easiest solution on paper, and it also makes sense to just hide the unwanted ValidationError border & text
.
So, my question, how do I make the style to hide the default WPF validation error (so the red border and the text behind it)? I'm a novice when it comes to styles, but this is what I've tried (and which doesn't do anything it seems):
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="HideValidationErrorContentControlStyle" TargetType="{x:Type ContentControl}" BasedOn="{StaticResource {x:Type ContentControl}}">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<!-- Empty -->
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<!-- Empty -->
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
With the style on my LookUp-ContentControl
like this:
<ContentControl x:Name="MyLookup" Style="{StaticResource HideValidationErrorContentControlStyle}"/>
This is the result I want (even when a ValidationError would normally be shown):
Upvotes: 2
Views: 438
Reputation: 4978
Maybe it isn't your LookUp-ContentControl
the one showing the error, but another control inside it. A TextBox
, maybe?
Also, instead of leaving the ErrorTemplate
empty, try to add an AdornedElementPlaceholder
.
<Style x:Key="HideValidationErrorContentControlStyle" TargetType="{x:Type ContentControl}" BasedOn="{StaticResource {x:Type ContentControl}}">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<AdornedElementPlaceholder />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
It's likely this isn't needed, but better be safe than sorry.
Upvotes: 1