Hodaya Shalom
Hodaya Shalom

Reputation: 4417

Get the property on Validation.Error event

Is it possible to get the binding for which fired the event: Validation.Error?

For example: I signed up for this event in a text box:

 <TextBox Validation.Error="My_Error" Text="{Binding MyProp,UpdateSourceTrigger=PropertyChanged,NotifyOnValidationError=True}" />

The event runs when there is a validation error and reaches the following function:

private void My_Error(object sender, ValidationErrorEventArgs e)
{
  //Here I want to get the property for which fired the event (MyProp). Is it possible?
}

Upvotes: 0

Views: 467

Answers (1)

Hodaya Shalom
Hodaya Shalom

Reputation: 4417

I found a way to do it:

        viewmodel Vm = (e.Error.BindingInError as BindingExpression).DataItem as viewmodel ;// Take viem model from data item. (I think that data item is the binding of the window - not sure)

        string propName= (e.Error.BindingInError as BindingExpression).ParentBinding.Path.Path;// The path is the prop name

        System.Reflection.PropertyInfo prop = Vm.GetType().GetProperty(propName);// Here the prop

        var valProp = prop.GetValue(Vm, null);//Here the value

Upvotes: 3

Related Questions