Question

My doubt is simple, how can I show an exception using this INotifyDataErrorInfo in WPF 4.5?

I'm using MVVM:

Here is the part of my view

    <TextBox MinHeight="50"
             Text="{Binding Person.Name, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}"

And here my model class. Check the Validate method, where I set @ character, should throw an exception

public class Person : DomainObject
{
    private string _name;

    public string Name
    {
        get
        {
            return this._name;
        }

        set
        {
            if (this._name != value)
            {
                this.ValidateProperty("Name", value);
                this._name = value;
                this.RaisePropertyChanged("Name");
            }
        }
    }
}

    protected override void ValidateProperty(string propertyName, object value)
    {
        if (propertyName == "Name")
        {
            var errors = new List<string>();

            var response = value as string;

            if (string.IsNullOrEmpty(response))
            {
                errors.Add("The value cannot be null or empty");
            }
            else if (response == "@")
            {
                throw new Exception("@");
            }

            this.ErrorsContainer.SetErrors(propertyName, errors);
        }
        else
        {
            base.ValidateProperty(propertyName, value);
        }
    }

When that happens, it really stop the programs.. And according to my knowledge, in Silverlight does not happen this.

Était-ce utile?

La solution

You probably use your setter somewhere else besides the bindings (and you don't catch the exception).

You need to run your application in debug mode. Visual Studio will show you the Exception assistant when the exception occurs.

Then you'll be able to analyze the stack trace and see how your program calls this code.

If it doesn't solve your problem, please update your question with the stack trace of the exception (Visual studio calls it "Unhandled Exception") that causes your program to stop.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top