It's quite easy https://stackoverflow.com/a/565560/1997232 to check if certain container or its children have validation errors. This can be used to disable Save button.
我可以使用计时器
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" ... />
有没有更好的办法?理想情况下,我想要一个活动。不幸的是我发现的唯一事件,验证错误 https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.validation.error事件,只能在具有绑定本身的元素上使用。浏览儿童元素并订阅(更不用说我必须处理添加新儿童的问题)感觉比投票更糟糕。
想法?
我通常处理这个问题的方式如下所示:
https://social.technet.microsoft.com/wiki/contents/articles/28597.aspx https://social.technet.microsoft.com/wiki/contents/articles/28597.aspx
错误事件将冒泡到容器中,您可以处理它,使用行为或命令将其传递到视图模型。
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>
任何绑定都需要 NotifyOnValidationError=True。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)