Brandon Heng
Brandon Heng

Reputation: 119

idataerrorinfo in c# WPF

I have implement idataerrorinfo as below but it does not work can you all help me?It do not appear the error on the screen when i debug.

The model part is :

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");
        }
    }
}

The Viewmodelbase part is:

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;
        }
    }


}

the ViewModel part is:

class MainWindowViewModel : Viewmodelbase,INotifyPropertyChanged
{
  public Person myboy
    {
        get;
        set;
    }
 }

WPF part is:

<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>

Upvotes: 2

Views: 245

Answers (1)

Liero
Liero

Reputation: 27400

C# is case sensitive.

It looks like you are using lower case for property name:

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

but you use CamelCase when storing the error:

this.seterror("Name", "Please Input sth");

Use CamelCase for properties and methods.

even the propertyname passed to seterror resp cleanerror and Onpropertychanged does not match:

clearerror("Name");
OnpropertyChange("name"); 

Please, follow the naming convention. naming convention makes sense. Next time I won't bother to answer question if you post code like this ;)

here is some inspiration for you:

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. 

Upvotes: 1

Related Questions