2013-06-30 50 views
0

是否有可能獲取激發事件的綁定:Validation.Error獲取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

我找到了方法:

 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