wpf - 如果验证失败,请禁用 WPF 中的“保存”按钮

标签 wpf validation save

我采用了使用 IDataErrorInfo 接口(interface)和样式来验证 WPF 中文本框的标准方法,如下所示。但是,当页面失效时,如何禁用“保存”按钮呢?这是通过触发器以某种方式完成的吗?

Default Public ReadOnly Property Item(ByVal propertyName As String) As String Implements IDataErrorInfo.Item
    Get
        Dim valid As Boolean = True
        If propertyName = "IncidentCategory" Then
            valid = True
            If Len(IncidentCategory) = 0 Then
                valid = False
            End If
            If Not valid Then
                Return "Incident category is required"
            End If
        End If

        Return Nothing

    End Get
End Property

<Style TargetType="{x:Type TextBox}">
    <Setter Property="Margin" Value="3" />
    <Setter Property="Height" Value="23" />
    <Setter Property="HorizontalAlignment" Value="Left" />
    <Setter Property="Validation.ErrorTemplate">
        <Setter.Value>
            <ControlTemplate>
                <DockPanel LastChildFill="True">
                    <Border BorderBrush="Red" BorderThickness="1">
                        <AdornedElementPlaceholder Name="MyAdorner" />
                    </Border>
                </DockPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="true">
            <Setter Property="ToolTip"  Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" />
        </Trigger>
    </Style.Triggers>
</Style>

最佳答案

有几件事:

首先,我建议使用 RoutedCommand ApplicationCommands.Save用于实现保存按钮的处理。

如果您还没有查看过 WPF Command 模型,您可以获取独家新闻 here .

<Button Content="Save" Command="Save">

现在,要实现该功能,您可以将命令绑定(bind)添加到 Window/UserControl 或按钮本身:

    <Button.CommandBindings>
        <CommandBinding Command="Save" 
                        Executed="Save_Executed" CanExecute="Save_CanExecute"/>
    </Button.CommandBindings>
</Button>

在代码隐藏中实现这些:

private void Save_Executed(object sender, ExecutedRoutedEventArgs e)
{
}

private void Save_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
}

Save_CanExecute ,设置e.CanExecute基于文本框绑定(bind)的有效性。

如果您想使用 MVVM(模型- View - View 模型)设计模式来实现,请查看 Josh Smith 在 CommandSinkBinding 上的帖子.

最后一点:如果您希望启用/禁用在 TextBox 中的值立即更新。已更改,设置UpdateSourceTrigger="PropertyChanged"关于 TextBox 的绑定(bind).

编辑:如果您想根据控件中的所有绑定(bind)进行验证/无效,这里有一些建议。

1) 您已经在实现 IDataErrorInfo 。尝试实现IDataErrorInfo.Error属性,这样它返回的字符串对于您绑定(bind)到的所有属性都无效。仅当您的整个控件绑定(bind)到单个数据对象时,这才有效。设置e.CanExecute = string.IsNullOrEmpty(data.Error);

2) 使用反射获取相关控件上的所有公共(public)静态 DependencyProperties。然后调用BindingOperations.GetBindingExpression(relevantControl, DependencyProperty)在每个属性上循环,以便您可以测试验证。

3) 在构造函数中,手动创建嵌套控件上所有绑定(bind)属性的集合。在 CanExecute 中,迭代此集合并验证每个 DependencyObject/DepencyProperty使用 BindingOperation.GetBindingExpression() 进行组合获取表达式,然后检查 BindingExpression.HasError .

关于wpf - 如果验证失败,请禁用 WPF 中的“保存”按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/757590/

相关文章:

c# - WPF 应用程序与 Winforms 应用程序有什么区别?

c# - 自定义控件的基类

javascript - 正则表达式仅包含数字和空格

java - Selenium - 保存网站,包括所有图像、css、dom

ios - 在swift3中将图像缩放到更小的尺寸

ios - iPhone 上的视频保存在哪里?

c# - 鼠标按下事件触发两次(WPF)

wpf - 在VB.net和WPF中处理异常

php - 在 CakePHP 中验证和转换十进制值

php - 验证我的表单和标题到另一个成功页面