2013-07-01 19 views
1

我有一個文本框: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在代碼隱藏它的工作原理:

enter image description here

但是,當我嘗試這種方式與事件命令運行它爲n未來有效功能。

我錯過了什麼嗎?

回答

2

使用驗證由於Validation.Error附加事件,那麼它不與EventToCommand正常工作。

,你會發現下面的鏈接答案:

EventToCommand with attached event

0

對於TextBox沒有Validation.Error事件。此外,System.Controls.TextBox(您正在使用)沒有Validating事件。

使用LostFocus驗證文本框或看this question,如果你想與MVVM模式

+0

事實上,有Validation.Error到一個文本框。看看有問題的圖片。它在代碼隱藏中起作用。我對迷失焦點不感興趣 - 驗證已在工作。我想在發生驗證錯誤時運行命令。 –

+0

不,不存在'TextBox'的'Validation.Error' *事件*。您正在嘗試將它用作* event *,這是錯誤的。 –

+0

當代碼隱藏 - 這不是一個事件? –