Search code examples
c#wpfvalidationglobalevent

Monitor for all validation events


It's quite easy to check if certain container or its children have validation errors. This can be used to disable Save button.

I can use timer

public SomeUserControl()
{
    InitializeComponent();
    var timer = new DispatcherTimer
    {
        Interval = TimeSpan.FromMilliseconds(100),
        IsEnabled = true
    };
    Loaded += (s, e) => buttonSave.IsEnabled = IsValid(grid);
    Unloaded += (s, e) => timer.Stop();
}

to poll and to disable button.

<!-- container with lots of controls, bindings and validations -->
<Grid x:Name="grid">
   ...
</Grid>

<!-- save button -->
<Button x:Name="buttonSave" ... />

Is there a better way? Ideally I want an event. Unfortunately the only event I've found, Validation.Error event, can only be used on the element with bindings itself. Going through children elements and subscribing (not mentioning what I have to deal with adding new children) feels way worser than polling.

Thoughts?


Solution

  • The way I usually handle this is illustrated here:

    https://social.technet.microsoft.com/wiki/contents/articles/28597.aspx

    The errorevent will bubble to a container and you can handle that, use a behavior or command to pass it to the viewmodel.

    Like:

    <ControlTemplate x:Key="AddingTriggers" TargetType="ContentControl">
        <ControlTemplate.Resources>
            <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource ErrorToolTip}">
                <Setter Property="HorizontalAlignment" Value="Left"/>
            </Style>
    
        </ControlTemplate.Resources>
        <StackPanel>
            <i:Interaction.Triggers>
                <local:RoutedEventTrigger RoutedEvent="{x:Static Validation.ErrorEvent}">
                    <e2c:EventToCommand   Command="{Binding ConversionErrorCommand, Mode=OneWay}"
                                            EventArgsConverter="{StaticResource BindingErrorEventArgsConverter}"
                                            PassEventArgsToCommand="True" />
                </local:RoutedEventTrigger>
            </i:Interaction.Triggers>
            <TextBlock Text="This would be some sort of a common header" Foreground="LightBlue" HorizontalAlignment="Right"/>
            <ContentPresenter/> <!-- This is how you can have variable content "within" the control -->
            <TextBlock Text="This would some sort of a common footer" Foreground="LightBlue"  HorizontalAlignment="Right"/>
        </StackPanel>
    </ControlTemplate>
    

    You need NotifyOnValidationError=True on any bindings.