In my WPF application, I want to only show the validation adorner after a control has been edited/entered/focused by the user. This way the user is given a chance to provide valid input into the field and only if they chose not to, then the validation will display.
We want to encourage every field to be completed so indicating mandatory fields when the form first opens may circumvent that as immediately the user will be inclined to just complete what they need to in order to get rid of the big red validation errors which may also circumvent the flow of the form.
Is there a way to know if a control has held focus yet? Would an attached property maybe work?
In case it helps provide a more concrete response: here is my current validation style that displays a red border [if the control has a border] and little exclamation mark with a tooltip for the error message (pretty standard really):
<Style TargetType="Control">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={x:Static RelativeSource.Self},
Path=(Validation.Errors).CurrentItem.ErrorContent}"/>
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel LastChildFill="true">
<Image Source="../Resources/Icons/Error.ico" Margin="4" Width="15" ToolTip="{Binding ElementName=customAdorner, Path=AdornedElement.(Validation.Errors).CurrentItem.ErrorContent}" />
<AdornedElementPlaceholder Name="customAdorner" VerticalAlignment="Center" >
<Border BorderBrush="red" BorderThickness="1" Visibility="{Binding ElementName=customAdorner, Path=AdornedElement.BorderThickness, Converter={StaticResource hasBorderToVisibilityConverter}}" />
</AdornedElementPlaceholder>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsVisible" Value="False">
<Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/>
</Trigger>
</Style.Triggers>
</Style>
You could combine an Attached Behavior with an Attached Property to do this. The Attached Behavior,
ObserveFocus
will subscribe to theGotFocus
event and in the event handler set theHasHeldFocus
Attached Property toTrue
It could be used to set a Property in a ViewModel like this
Here is an example of how it could be used to change the
Background
of aButton
once it has been FocusedHasHeldFocusBehavior
Update
In your case, you could replace the
Validation.HasError
Trigger with a MultiTrigger