How to fire a textbox command upon losing focus in WPF?

后端 未结 1 1173
北恋
北恋 2021-01-28 04:57

The following template works fine if the user presses Enter, although I want the command to fire when the TextBox loses focus as well. How can I do this?

相关标签:
1条回答
  • 2021-01-28 05:40

    If you don't use any MVVM framework you could use InvokeCommandAction from System.Windows.Interactivity to execute command when event is fired

    <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>
    

    you'll need to add reference to System.Windows.Interactivity and add namespace in your XAML:

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

    0 讨论(0)
提交回复
热议问题