2017-10-12 73 views
0

觸發我的問題是交互觸發器不能在Keyboard.LostKeyboardFocus事件上工作。但是當我從後面的代碼中完成工作。 我有一個文本框與下面的代碼,交互觸發器不在Keyboard.LostKeyboardFocus

     <TextBox 
         Text="{Binding PostRunPlateNotes, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
         Height="113" 
         Width="Auto" 
         ScrollViewer.HorizontalScrollBarVisibility="Auto" 
         ScrollViewer.VerticalScrollBarVisibility="Auto"> 
         <i:Interaction.Triggers> 
          <i:EventTrigger 
           EventName="Keyboard.LostKeyboardFocus"> 
           <i:InvokeCommandAction 
            Command="{Binding SavePlateNotesCommand}"></i:InvokeCommandAction> 
          </i:EventTrigger> 
         </i:Interaction.Triggers> 
        </TextBox> 

而且在這裏的視圖模型是代碼,

#region CommandData 
     private ICommand _savePlateNotesCommand; 
     public ICommand SavePlateNotesCommand 
     { 
      get 
      { 
       return _savePlateNotesCommand ?? (_savePlateNotesCommand = new RelayCommand(OnSaveClick)); 
      } 
      set 
      { 
       _savePlateNotesCommand = value; 
       OnPropertyChanged("SavePlateNotesCommand"); 
      } 
     } 
     #endregion 

在後面的代碼,

public partial class RunInformationUserControl : UserControl, INotifyPropertyChanged 
    { 
     #region Accessors 
     public RunInformationParameters RunInfoParams { get; set; } 
     public RunInformationViewModel RunViewModel { get; set; } 
     #endregion 

     #region Constructor 
     public RunInformationUserControl() 
     { 
      InitializeComponent(); 
      RunViewModel = null; 
     } 
     #endregion 

     #region Methods 
     public void SetParameters(RunInformationParameters parameters) 
     { 
      this.RunInfoParams = parameters; 
      RunViewModel = new RunInformationViewModel(RunInfoParams); 
      this.DataContext = RunViewModel; 
     } 

     #endregion 

     #region EventHandlers 
     private void UserControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e) 
     { 
      if (RunViewModel != null) 
      { 
       OnPropertyChanged("runInfoParams"); 
      } 
     } 

     #endregion 

     #region INotifyPropertyChangedEvent 
     public event PropertyChangedEventHandler PropertyChanged; 
     protected virtual void OnPropertyChanged(string propertyName) 
     { 
      PropertyChangedEventHandler handler = PropertyChanged; 
      if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); 
     } 

     #endregion 

    } 

問題是,當我提高鍵盤在代碼後面失去焦點。所有其他屬性都被正確綁定。但交互觸發器不起作用,請幫助。

+0

如果在XAML中將'Keyboard.LostKeyboardFocus'更改爲'LostKeyboardFocus',它會工作嗎? –

+0

@SzabolcsDézsi感謝它的工作方式。 – nikhil

回答

0

內部的EventTrigger最終會在它的源代碼中調用typeof(TextBox).GetEvent("Keyboard.LostKeyboardFocus"),並且將返回null,因爲TextBox沒有Keyboard.LostKeyboardFocus事件。由於Keyboard.LostKeyboardFocus是附加事件,因此WPF/XAML概念GetEvent無法將其返回,並且EventTrigger無法訂閱它。

另一方面TextBoxUIElement繼承LostKeyboardFocus,所以使用它將工作。