获取Validation.Error事件的属性

时间:2013-06-30 08:10:12

标签: c# wpf validation binding

是否可以获取触发事件的绑定:Validation.Error

例如:我在文本框中注册了此活动:

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

当出现验证错误并且达到以下功能时,事件将运行:

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

1 个答案:

答案 0 :(得分:2)

我找到了办法:

        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