2014-09-24 68 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

相關問題