2014-09-24 158 views
0

如果用户按下输入,但以下模板可以正常工作,但我也希望在TextBox失去焦点时触发该命令。我怎样才能做到这一点?如何在WPF中失去焦点时触发文本框命令?

以下是我TextBox模板

<DataTemplate x:Key="PhantomNameEditTemplate">   
    <TextBox Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}"> 
     <TextBox.InputBindings>     
      <KeyBinding Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=DataGrid}, Path=DataContext.RenameCommand}" CommandParameter="{Binding}" Key="Enter" /> 
     </TextBox.InputBindings> 
    </TextBox> 
</DataTemplate> 

回答

4

如果你不使用任何MVVM框架,你可以使用InvokeCommandActionSystem.Windows.Interactivity执行命令时,事件被触发

<TextBox ...> 
    <i:Interaction.Triggers> 
     <i:EventTrigger EventName="LostFocus"> 
      <i:InvokeCommandAction 
       Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=DataGrid}, Path=DataContext.RenameCommand}" 
       CommandParameter="{Binding}"/> 
     </i:EventTrigger> 
    </i:Interaction.Triggers> 
</TextBox> 

你需要添加对System.Windows.Interactivity的引用并在您的XAML中添加命名空间:

xmlns:i =“http://schemas.microsoft.com/expression/2010/interactivity”

+1

谢谢,我没有使用MVVM框架,并且工作完美。 – Bob 2014-09-24 14:40:16

相关问题