我已经实现了如下的idataerrorinfo,但是它不能正常工作,请大家帮帮我吗?我调试时在屏幕上没有出现错误。

模型部分是:

class Person : Viewmodelbase, INotifyPropertyChanged{
    private string name;
    private string age;
    public string Name{
        get
        {
            return name;
        }

        set
        {
            name = value;
            if (string.IsNullOrWhiteSpace(value))
            {
                this.seterror("Name", "Please Input sth");
            }
            else
                clearerror("Name");
            OnpropertyChanged("name");
        }
    }
}

Viewmodelbase部分是:
class Viewmodelbase : INotifyPropertyChanged, IDataErrorInfo
{
    public event PropertyChangedEventHandler PropertyChanged;

    public void onpropertychange(string property)
    {
        if (string.IsNullOrEmpty(property))
           PropertyChanged(this, new PropertyChangedEventArgs(property));
    }

    string IDataErrorInfo.Error
    {
        get
        {
            throw new NotImplementedException();
        }
    }

    private Dictionary<string, string> error1 = new Dictionary<string, string>();

    public string this[string columnName]
    {
        get
        {
            if (error1.ContainsKey(columnName))
                return error1[columnName];
            return null;

        }
    }

    public void seterror(string key1,string value1)
    {
        this.error1[key1] = value1;
        this.onpropertychange("HasError");
    }

    public void clearerror(string key1)
    {
        if (this.error1.ContainsKey(key1))
        {
            this.error1.Remove(key1);
            this.onpropertychange("HasError");
        }
    }

    protected void ClearErrors()
    {
        this.error1.Clear();
        this.onpropertychange("HasError");
    }

    public bool HasError
    {
        get
        {
            return this.error1.Count != 0;
        }
    }


}

ViewModel部分是:
class MainWindowViewModel : Viewmodelbase,INotifyPropertyChanged
{
  public Person myboy
    {
        get;
        set;
    }
 }

WPF部分是:
<TextBox Width="250" FontSize="20" VerticalAlignment="Center" DataContext="{Binding Source={StaticResource VM}}" Text="{Binding Path=myboy.Name,Mode=TwoWay,ValidatesOnDataErrors=True,UpdateSourceTrigger=PropertyChanged,NotifyOnValidationError=True,ValidatesOnExceptions=True}"  Name="textbox1" Margin="0,199"></TextBox>

最佳答案

C#区分大小写。

似乎您将小写字母用作属性名称:

public string name{ get{..} set{..} }

但是在存储错误时使用CamelCase:
this.seterror("Name", "Please Input sth");

使用CamelCase获取属性和方法。

甚至传递给seterrorcleanerrorOnpropertychanged的属性名也不匹配:
clearerror("Name");
OnpropertyChange("name");

请遵循命名约定。命名约定很有意义。下次,如果您发布这样的代码,我将不愿意回答问题;)

这是给您的一些启示:
private string _name; //or `private string name;` but I recommend underscore for private fields.
public string Name{get; set;}
public Person MyBoy{get; set;}
public void OnPropertyChanged(string propertyName); //propertyChange and PropertyChanged has different meaning, so use the correct one
public void SetError(string propertyName, string error){..} //parameter names key and value are useless, they say nothing about what the parameters actually are.

关于c# - C#WPF中的idataerrorinfo,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38309502/

10-10 15:10