C# 为什么绑定到文本框的命令不能在wpf中激发?

C# 为什么绑定到文本框的命令不能在wpf中激发?,c#,wpf,xaml,C#,Wpf,Xaml,我已经使用触发器将文本框绑定到命令,但是当用户在键盘上点击enter键时,似乎什么也没有发生。为什么会这样?我想要发生的是,当用户点击enter键时,它会将名称附加到列表并清除输入字段 主要关注领域如下: xaml片段 <TextBox Grid.Row="0" Text="{Binding InputText, UpdateSourceTrigger=PropertyChanged}"> <TextBox.InputBindings>

我已经使用触发器将文本框绑定到命令,但是当用户在键盘上点击enter键时,似乎什么也没有发生。为什么会这样?我想要发生的是,当用户点击enter键时,它会将名称附加到列表并清除输入字段

主要关注领域如下: xaml片段

   <TextBox Grid.Row="0" Text="{Binding InputText, UpdateSourceTrigger=PropertyChanged}">
            <TextBox.InputBindings>
                <KeyBinding Key="Enter" Command="{Binding Path=CmdSomething, UpdateSourceTrigger=PropertyChanged}"/>
            </TextBox.InputBindings>
        </TextBox>
下面是整个项目的代码,以防有人需要它进行测试

项目代码

VNodes.cs

namespace WpfApplication1
{
    public class VNode
    {
        public string Name { get; set; }
    }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace WpfApplication1
{
    public class ObservableObject : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
main window.xaml

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        xmlns:local="clr-namespace:WpfApplication1">

    <Window.DataContext>
        <local:MainViewModel/>
    </Window.DataContext>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="30"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <TextBox Grid.Row="0" Text="{Binding InputText, UpdateSourceTrigger=PropertyChanged}">
            <TextBox.InputBindings>
                <KeyBinding Key="Enter" Command="{Binding Path=CmdSomething, UpdateSourceTrigger=PropertyChanged}"/>
            </TextBox.InputBindings>
        </TextBox>

        <ListBox Grid.Row="1" Background="LightBlue" ItemsSource="{Binding VNodes}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <WrapPanel>
                        <TextBlock Text="{Binding Name}" FontWeight="Bold" />
                    </WrapPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.CompilerServices;
using System.Collections.ObjectModel;


namespace WpfApplication1
{
    public class MainViewModel : ObservableObject
    {
        private ObservableCollection<VNode> _vnodes;
        public ObservableCollection<VNode> VNodes
        {
            get { return _vnodes; }
            set
            {
                _vnodes = value;
                NotifyPropertyChanged("VNodes");
            }
        }

        private string _inputText = "";
        public string InputText
        {
            get { return _inputText; }
            set
            {
                if (value != _inputText)
                {
                    _inputText = value;
                }
            }
        }

        public void CmdSomething()
        {
            Console.WriteLine(InputText);
        }

        public MainViewModel()
        {
            //hard coded data for testing
            VNodes = new ObservableCollection<VNode>();
            List<string> names = new List<string>() { "Tammy", "Doug", "Mike", "Joey", "Leslie", "Emily", "Tom" };

            foreach(string name in names)
            {
                VNode item = new VNode();
                item.Name = name;
                VNodes.Add(item);
            }
        }
    }
}

CMD代码中的某些内容不是ICommand,而是一个函数

现在有两种方法可以使它在您的案例中起作用

创建一个ICommand。太好了

如果MyCommand是ViewModel中ICommand类型的属性,则要绑定自定义命令:

<TextBox.InputBindings>
      <KeyBinding Key="Enter" Command="{Binding MyCommand, Mode=OneWay}" />
</TextBox.InputBindings>

如果你想调用你的函数,那么你可以使用行为。你可以在谷歌上搜索更多教程

对于您当前的问题,这是没有任何问题的:

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



@JokerMartini您是否设置了CommandTarget=“{Binding ElementName=MyTextBox}”,但由于某种原因,它似乎仍然不起作用。@JokerMartini当您说它不起作用时,请告诉错误/异常/屏幕截图。您可以使用screentogif转换器实用程序。将建议的解决方案放入我的代码中时不会发生任何事情。函数永远不会被调用executed@JokerMartini我将制作一个示例应用程序,并将在2小时后上传到dropbox。
<TextBox.InputBindings>
      <KeyBinding Key="Enter" Command="{Binding MyCommand, Mode=OneWay}" />
</TextBox.InputBindings>
       <TextBox x:Name="textBox" Margin="144,80,288,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Height="32">
            <i:Interaction.Triggers>
                <ei:KeyTrigger Key="Return"> <!-- Note Blend Behaviors don't use Enter, instead use Return value -->
                    <ei:CallMethodAction TargetObject="{Binding DataContext, RelativeSource={RelativeSource AncestorType={x:Type Window}, Mode=FindAncestor}}" MethodName="CmdSomething"/>
                </ei:KeyTrigger>
            </i:Interaction.Triggers>
        </TextBox>