wpf 强制 IDataErrorInfo 验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13378474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:12:08  来源:igfitidea点击:

Force IDataErrorInfo validation

wpfvalidationidataerrorinfo

提问by Witcher

I have two controls on some panel: textbox and combobox:

我在某些面板上有两个控件:文本框和组合框:

<TextBox Text="{Binding ShapeName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" />

<ComboBox SelectedItem="{Binding ActiveStageViewModel, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" />

I need to force idataerrorinfo validation for textbox, when I select something on combobox. How to do that?

当我在组合框上选择某些内容时,我需要强制对文本框进行 idataerrorinfo 验证。怎么做?

The code from viewmodel:

视图模型中的代码:

string IDataErrorInfo.this[string propertyName]
    {
      get
      {
        var error = string.Empty;

        if (propertyName == Expression.GetPropertyName(() => ActiveStageViewModel))
        {
          // TODO: Add functionality to force ShapeName property validation
          return error;
        }

        if (propertyName == Expression.GetPropertyName(() => ShapeName))
        {
          error = ValidateShapeName();
        }

        TooltipMessage = error;
        return error;
      }
    }

回答by bitbonk

Just raise the NotifyPropertyChangedevent for the property you want to validate or if you are in .NET 4.5 or Silverlight 4 use the INotityDataErrorInfo.

只需NotifyPropertyChanged为要验证的属性引发事件,或者如果您使用 .NET 4.5 或 Silverlight 4,请使用INotityDataErrorInfo

回答by Paul Marques

Worth remembering also that you can raise NotifyPropertyChanged with String.Empty property. This will force validation of all properties at that level. Can be useful at times.

还值得记住的是,您可以使用 String.Empty 属性引发 NotifyPropertyChanged。这将强制验证该级别的所有属性。有时可能很有用。