我有一個文本框:Validation.error事件
<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>
我的視圖模型是這樣的:
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在代碼隱藏它的工作原理:
但是,當我嘗試這種方式與事件命令運行它爲n未來有效功能。
我錯過了什麼嗎?
事實上,有Validation.Error到一個文本框。看看有問題的圖片。它在代碼隱藏中起作用。我對迷失焦點不感興趣 - 驗證已在工作。我想在發生驗證錯誤時運行命令。 –
不,不存在'TextBox'的'Validation.Error' *事件*。您正在嘗試將它用作* event *,這是錯誤的。 –
當代碼隱藏 - 這不是一個事件? –