【发布时间】:2017-01-31 09:37:05
【问题描述】:
我正在开发一个 WPF 应用程序,在该应用程序中我使用 IDataErrorInfo 和验证规则进行验证。 为了在运行时显示验证结果,我在窗口的 XAML 中创建了一些样式。 只要有输入错误,其中一种样式应该禁用保存按钮:
<Window.Resources>
<!--Disabling the Save-button by style not viewmodel-property-->
<Style TargetType="{x:Type Button}">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=tbx_firstname, Path=(Validation.HasError)}" Value="true">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=tbx_lastname, Path=(Validation.HasError)}" Value="true">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=tbx_age, Path=(Validation.HasError)}" Value="true">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
我有两个问题:
-
该样式禁用窗口上的所有按钮。我该怎么做才能只禁用保存按钮? 我尝试了以下方法,但似乎存在语法错误(VS 不接受):
<Style TargetType="{x:Name btn_save}"> 在保存按钮的样式中,我必须检查每个经过验证的控件。 是否有另一种可能使该部分更短且不易出错(因为它必须与视图模型结合)?
提前致谢!
【问题讨论】:
标签: wpf validation xaml idataerrorinfo