文本框输入键事件在 WPF 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15487902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:09:18  来源:igfitidea点击:

Textbox Enter Key event not working in WPF

c#wpf

提问by DewSql

The keydown event is not working properly. I want to raise the same event as the button when the enter key is pressed. Here is the c#

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 = "";
            }
        }
    }

采纳答案by Dilshod

There is no need to write the code two times. You could do like this too.

不需要写两次代码。你也可以这样做。

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);
    }
}

回答by Harshil

in wpf there is no keycode or keys.enter instead you can use :

在 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();
    }
}

回答by Evgeniy Miroshnichenko

In WPF, TextBox element will not get opportunity to use "Enter" button for creating KeyUp Event until you will not set property: AcceptsReturn="True".

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

But, it would`t solve the problem with handling KeyUp Event in TextBox element. After pressing "ENTER" you will get a new text line in TextBox.

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

I had solved problem of using KeyUp Event of TextBox element by using Bubble event strategy. It's short and easy. You have to attach a KeyUp Event handler in some (any) parent element:

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

XAML:

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# logical part (KeyUp Event handler is attached to a grid element):

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);
            }
        }
    }
}

RESULT:

结果:

Application main window with result

带有结果的应用程序主窗口