Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 按enter键时在文本框上执行命令_C#_Wpf_Xaml_Mvvm - Fatal编程技术网

C# 按enter键时在文本框上执行命令

C# 按enter键时在文本框上执行命令,c#,wpf,xaml,mvvm,C#,Wpf,Xaml,Mvvm,我是WPF的新手,我看到了最好的模式调用MVVM。我试着深入了解它,我发现该命令只能在按钮或菜单项上执行,但我怀疑当我关注文本框时如何执行ViewModel命令,并在完成编辑时按enter键。 我有谷歌这个,但我没有从所有的答案。希望你们都能帮助我。在文本框中按enter键时如何执行命令?您可以使用WPF中的行为来实现您的需求 在XAML中 xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.In

我是WPF的新手,我看到了最好的模式调用MVVM。我试着深入了解它,我发现该命令只能在按钮或菜单项上执行,但我怀疑当我关注文本框时如何执行ViewModel命令,并在完成编辑时按enter键。
我有谷歌这个,但我没有从所有的答案。希望你们都能帮助我。在文本框中按enter键时如何执行命令?

您可以使用WPF中的行为来实现您的需求

在XAML中

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 

    <TextBox Text="MyText">
        <i:Interaction.Behaviors>
            <i:BehaviorCollection>
                <EventToCommand EventName="TextChanged" Command="{Binding ViewModelCommand}"> 
**// You can provide other events to be triggered in the EventName property based on your requirement like "Focused" or "UnFocused".Focused event will be fired if you enter into edit mode and UnFocused event will be triggered if you press enter key.**
            <i:BehaviorCollection>
        </i:Interaction.Behaviors>
    </TextBox>

您可以使用WPF中的行为来实现您的需求

在XAML中

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 

    <TextBox Text="MyText">
        <i:Interaction.Behaviors>
            <i:BehaviorCollection>
                <EventToCommand EventName="TextChanged" Command="{Binding ViewModelCommand}"> 
**// You can provide other events to be triggered in the EventName property based on your requirement like "Focused" or "UnFocused".Focused event will be fired if you enter into edit mode and UnFocused event will be triggered if you press enter key.**
            <i:BehaviorCollection>
        </i:Interaction.Behaviors>
    </TextBox>

在我看来,最简单的方法是使用,它允许您将绑定到实现

在您的情况下,您可以在XAML中编写如下内容:

<TextBox AcceptsReturn="False">
    <TextBox.InputBindings>
        <KeyBinding Key="Enter" Command="{Binding YourCommand}" />
    </TextBox.InputBindings>
</TextBox>

因此,当您的
文本框
处于焦点并按Enter键时,
您的命令
将被执行


我希望它能帮助您。

在我看来,最简单的方法是使用,它允许您将a绑定到实现

在您的情况下,您可以在XAML中编写如下内容:

<TextBox AcceptsReturn="False">
    <TextBox.InputBindings>
        <KeyBinding Key="Enter" Command="{Binding YourCommand}" />
    </TextBox.InputBindings>
</TextBox>

因此,当您的
文本框
处于焦点并按Enter键时,
您的命令
将被执行


我希望它能对您有所帮助。

看看这里,我想您要寻找的是一种附加行为,它使文本框的
Text
属性在您按下Enter键时更新:(同时阅读此答案的注释)。不需要命令,我猜…看看这里我猜你要找的是一个附加的行为,当你点击Enter键时,它会使文本框的
Text
属性更新:(同时阅读对这个答案的注释)。不需要命令,我猜…这将对您在textbox中键入的每个字符执行命令,而OP只需要在Enter上执行命令他可以使用textbox中的聚焦和非聚焦事件而不是TextChanged事件来实现他的要求这将对您在textbox中键入的每个字符执行命令,虽然OP只需要输入,但他可以使用TextBox中的聚焦和非聚焦事件而不是TextChanged事件来实现他的需求