【问题标题】:Textbox Enter Key event not working in WPF文本框输入键事件在 WPF 中不起作用
【发布时间】:2013-03-18 22:10:01
【问题描述】:

keydown 事件无法正常工作。我想在按下回车键时引发与按钮相同的事件。这是c#

    public partial class Search : Control
    {
        public SearchRevision()
        {
            InitializeComponent();
        }


        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            SearchViewModel vm = this.DataContext as SearchViewModel;
            if (vm != null)
                vm.Refresh();
        }

        private void myTextBox_KeyDown(Object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                SearchViewModel vm = this.DataContext as SearchViewModel;
                if (vm != null)
                    vm.Refresh();
            }
        }

        private void myTextBox_Escape(Object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                txtName.Text = "";
            }
        }
    }

【问题讨论】:

  • 错误信息是什么?在KeyDown 方法中放置一个断点,看看它是否被命中。此外,您应该将您的 ViewModel 代码移动到它自己的方法中,因为您重复相同的事情两次。从按钮单击处理程序和 keydown 处理程序调用此方法。
  • 对于 MVVM,这是answer

标签: c# wpf


【解决方案1】:

在 wpf 中没有 keycode 或 keys.enter 相反,您可以使用:

private void myTextBox_KeyDown(Object sender, KeyEventArgs e)
{
    if (e.Key == Key.Enter)
    {
        SearchViewModel vm = this.DataContext as SearchViewModel;
        if (vm != null)
            vm.Refresh();
    }
}

【讨论】:

    【解决方案2】:

    在 WPF 中,TextBox 元素将没有机会使用“Enter”按钮来创建 KeyUp 事件,除非您不会设置属性:AcceptsReturn="True"。

    但是,它不能解决在 TextBox 元素中处理 KeyUp 事件的问题。按“ENTER”后,您将在 TextBox 中获得一个新的文本行。

    我已经通过使用 Bubble 事件策略解决了使用 TextBox 元素的 KeyUp 事件的问题。它简短而容易。您必须在某些(任何)父元素中附加一个 KeyUp 事件处理程序:

    XAML:

    <Window x:Class="TextBox_EnterButtomEvent.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:TextBox_EnterButtomEvent"
            mc:Ignorable="d"
            Title="MainWindow" Height="350" Width="525">
        <Grid KeyUp="Grid_KeyUp">
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height ="0.3*"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Row="1" Grid.Column="1" Padding="0" TextWrapping="WrapWithOverflow">
                Input text end press ENTER:
            </TextBlock>
            <TextBox Grid.Row="2" Grid.Column="1" HorizontalAlignment="Stretch"/>
            <TextBlock Grid.Row="4" Grid.Column="1" Padding="0" TextWrapping="WrapWithOverflow">
                You have entered:
            </TextBlock>
            <TextBlock Name="txtBlock" Grid.Row="5" Grid.Column="1" HorizontalAlignment="Stretch"/>
        </Grid>
    </Window>
    

    C# 逻辑部分(KeyUp 事件处理程序附加到网格元素):

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    
        private void Grid_KeyUp(object sender, KeyEventArgs e)
        {
            if(e.Key == Key.Enter)
            {
                TextBox txtBox = e.Source as TextBox;
                if(txtBox != null)
                {
                    this.txtBlock.Text = txtBox.Text;
                    this.txtBlock.Background = new SolidColorBrush(Colors.LightGray);
                }
            }
        }
    }
    

    结果:

    Application main window with result

    【讨论】:

      【解决方案3】:

      在 WPF 中,有时“Key.enter”无法识别,您必须改用“System.Windows.Input.Key.Enter”

      if (e.Key == System.Windows.Input.Key.Enter)
      {
          //Your code here
      }
      

      我遇到了和 OP 一样的问题,这是对我有用的解决方案。

      【讨论】:

        【解决方案4】:

        无需编写两次代码。你也可以这样做。

        private void myTextBox_KeyDown(Object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                //you may pass the parameters if you need
                Button_Click_1(null,null);
            }
        }
        

        【讨论】:

        • 如何回答这个问题,因为答案是针对 Windows 窗体而问题是 WPF? KeyEventArgs.KeyCode 仅存在于 System.Windows.Forms.KeyEventArgs 上,而不存在于 WPF System.Windows.Input.KeyEventArgs 上,类名相同但命名空间不同,属性和方法不同。
        • 我同意 Rodney Foley 的观点,e.KeyCode 在 WPF 文本框中不存在。
        猜你喜欢
        • 1970-01-01
        • 2012-01-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多