监控所有验证事件
Monitor for all validation events
quite easy 检查某个容器或其子容器是否存在验证错误。这可用于禁用 保存 按钮。
我可以使用定时器
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();
}
轮询和禁用按钮。
<!-- container with lots of controls, bindings and validations -->
<Grid x:Name="grid">
...
</Grid>
<!-- save button -->
<Button x:Name="buttonSave" ... />
有没有更好的方法?理想情况下,我想要一个事件。不幸的是,我发现的唯一事件 Validation.Error 事件只能用于具有绑定本身的元素。遍历子元素和订阅(更不用说我必须处理添加新子元素的事情)感觉比轮询更糟糕。
想法?
这里说明了我通常处理此问题的方式:
https://social.technet.microsoft.com/wiki/contents/articles/28597.aspx
errorevent 将冒泡到容器中,您可以处理它,使用行为或命令将其传递给视图模型。
喜欢:
<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>
任何绑定都需要 NotifyOnValidationError=True。
quite easy 检查某个容器或其子容器是否存在验证错误。这可用于禁用 保存 按钮。
我可以使用定时器
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();
}
轮询和禁用按钮。
<!-- container with lots of controls, bindings and validations -->
<Grid x:Name="grid">
...
</Grid>
<!-- save button -->
<Button x:Name="buttonSave" ... />
有没有更好的方法?理想情况下,我想要一个事件。不幸的是,我发现的唯一事件 Validation.Error 事件只能用于具有绑定本身的元素。遍历子元素和订阅(更不用说我必须处理添加新子元素的事情)感觉比轮询更糟糕。
想法?
这里说明了我通常处理此问题的方式:
https://social.technet.microsoft.com/wiki/contents/articles/28597.aspx
errorevent 将冒泡到容器中,您可以处理它,使用行为或命令将其传递给视图模型。
喜欢:
<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>
任何绑定都需要 NotifyOnValidationError=True。