【问题标题】:Screen Validation With Data Annotations on Button Click in WPF在 WPF 中单击按钮时使用数据注释进行屏幕验证
【发布时间】:2016-08-11 12:14:20
【问题描述】:

我的 WPF 表单包含单个属性“PersonName”。我希望在 NULL OR EMPTY 上提出错误。我从 Data Annotations 得到了解决方案,我参考了教程http://www.c-sharpcorner.com/uploadfile/20c06b/screen-validation-with-data-annotations-in-wpf/

我的 XAML 是

<TextBox Text="{Binding PersonName, UpdateSourceTrigger=PropertyChanged,
 NotifyOnValidationError=True, ValidatesOnDataErrors=True}" />

<Button Content="Save" IsDefault="True" Command="{Binding SaveCommand}"
 IsEnabled="{Binding }" Width="150" Height="40"/>

我只能在 onPropertyChange 事件中看到错误。如果我直接点击按钮而不触摸TextBox 表示,我无法看到错误。我怎样才能在 Button 点击时触发相同的功能。

我的要求是 PersonName 不应该是 NULL 或 EMPTY,如果属性是 NULL 或 EMPTY,那么我需要禁用按钮,基于 IDataErrorInfo 而不是 Property.Length

最初,我们不应用任何验证。一旦错误的数据输入或没有点击按钮的值,那么我需要一个验证。

【问题讨论】:

    标签: c# wpf xaml data-annotations idataerrorinfo


    【解决方案1】:

    使用 IDataErrorInfo 接口并像下面这样实现它

    public class ABC : IDataErrorInfo
    {
        private string _PersonName;
        public string PersonName
        {
            get { return _PersonName; }
            set
            {
                _PersonName = value;
                OnPropertyChanged("PersonName");
            }
        }
    
        public string Error
        {
            get { return string.Empty; }
        }
    
        public string this[string columnName]
        {
            get
            {
                if ("PersonName" == columnName)
                {
                    if (String.IsNullOrEmpty(PersonName))
                    {
                        return "Your Error Message";
                    }
                }
            }
        }
    }
    

    并将xaml更改为

    <TextBox Text="{Binding PersonName, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, NotifyOnValidationError=True, ValidatesOnExceptions=True, ValidatesOnNotifyDataErrors=True}" />
    

    【讨论】:

    • ViewModel 类,其中包含 PersonName 属性。对该类实现 IDataErrorInfo 接口方法。你能分享包含 PersonName 属性的类名吗
    • 我在问在哪里绑定按钮单击中的验证触发器...?在按钮单击时我需要验证。
    • 在 Icommad 中有两种方法 Execute 和 CanExecute。您可以在 canexecute 方法中添加“return !String.IsNullOrEmpty(PersonName)”此代码。
    • 是的,我知道。我需要使用IDataErrorInfo 进行验证
    【解决方案2】:

    您可以将ValidationRule 应用于您的TextBox,如示例here 然后在你的Button上设置一个触发器

    <Button Content="Save" IsDefault="True" Command="{Binding SaveCommand} Width="150" Height="40">
        <Button.Style>
            <Style TargetType="Button">
              <Setter Property="IsEnabled" Value="False"/>
              <Style.Triggers>
                <MultiDataTrigger>
                  <MultiDataTrigger.Conditions>
                    <Condition Binding="{Binding Path=(Validation.HasError), ElementName=PersonName}" Value="False"/>
                  </MultiDataTrigger.Conditions>
                  <Setter Property="IsEnabled" Value="True"/>
                </MultiDataTrigger>
                </Style.Triggers>
              </Style>
        </Button.Style>
    </Button>
    

    【讨论】:

    • 如何将它与 Button 绑定?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-30
    • 2021-01-16
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多