我有一个文本框:
<TextBox Height="20" Width="150" Text="{Binding MyProperty,NotifyOnValidationError=True,ValidatesOnDataErrors=True}" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="Validation.Error">
<mvvm:EventToCommand Command="{Binding MyCmd}" PassEventArgsToCommand="True" ></mvvm:EventToCommand>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
我的 ViewModel 看起来像这样:
public class MyViewModel : ValidationViewModelBase, INotifyPropertyChanged
{
private int myVar;
[Range(0, 10)]
public int MyProperty
{
get { return myVar; }
set
{
myVar = value;
OnPropertyChanged("MyProperty");
}
}
public MyViewModel()
{
MyCmd = new RelayCommand<RoutedEventArgs>(Valid);
}
public RelayCommand<RoutedEventArgs> MyCmd { get; set; }
private void Valid(RoutedEventArgs args)
{
//Do something
}
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
#endregion INotifyPropertyChanged
}
当我在代码中捕获事件 Validation.Error 时,它的工作原理:
但是,当我尝试使用 Event Command 以这种方式运行它时,不会出现 Valid 函数。
我错过了什么?