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

时间:2013-03-18 22:10:01

标签: c# wpf

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

4 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:1)

在WPF中,TextBox元素将无法使用“Enter”按钮创建KeyUp事件,直到您不设置属性:AcceptsReturn =“True”。

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

我通过使用Bubble事件策略解决了使用TextBox元素的KeyUp Event的问题。这很简单。您必须在某个(任何)父元素中附加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

答案 2 :(得分:0)

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

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

我遇到了与 OP 相同的问题,这是对我有用的解决方案。

答案 3 :(得分:-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);
    }
}