Monitor for all validation events

别等时光非礼了梦想. 提交于 2019-12-13 07:47:52

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/58431927/monitor-for-all-validation-events

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!