如果验证失败,请在WPF中禁用"保存"按钮

Mit*_*tch 21 validation wpf save

我采用了似乎是使用IDataErrorInfo接口和样式验证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
Run Code Online (Sandbox Code Playgroud)

<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>
Run Code Online (Sandbox Code Playgroud)

Jos*_*h G 38

有几件事:

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

如果您还没有签出WPF Command模型,可以在这里获得独家新闻.

<Button Content="Save" Command="Save">
Run Code Online (Sandbox Code Playgroud)

现在,要实现该功能,您可以添加命令绑定到Window/UserControl或Button本身:

    <Button.CommandBindings>
        <CommandBinding Command="Save" 
                        Executed="Save_Executed" CanExecute="Save_CanExecute"/>
    </Button.CommandBindings>
</Button>
Run Code Online (Sandbox Code Playgroud)

在后面的代码中实现这些:

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

private void Save_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
}
Run Code Online (Sandbox Code Playgroud)

Save_CanExecute,e.CanExecute根据文本框上绑定的有效性进行设置.

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

最后一点注意事项:如果您希望在TextBox更改中的值后立即更新启用/禁用,请UpdateSourceTrigger="PropertyChanged"在绑定上设置TextBox.

编辑:如果你想根据控件中的所有绑定验证/无效,这里有一些建议.

1)您已经在实施IDataErrorInfo.尝试实现该IDataErrorInfo.Error属性,使其返回对您绑定的所有属性无效的字符串.这仅在整个控件绑定到单个数据对象时才有效.组e.CanExecute = string.IsNullOrEmpty(data.Error);

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

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

  • +1表示建议的命令用法.命令是WPF开始真正为我点击的原因. (2认同)