【问题标题】:How can I detect my keyboard without press enter C# WPF如何在不按 Enter C# WPF 的情况下检测我的键盘
【发布时间】:2018-06-06 19:40:24
【问题描述】:

如何将此代码更改为主动检测我的键盘。现在它显示了我按回车后写的内容。我怎样才能显示我可以在没有输入键的情况下写什么。

XAML:

<StackPanel>
  <TextBlock Width="300" Height="20">
   Type some text into the TextBox and press the Enter key.
  </TextBlock>
  <TextBox Width="300" Height="30" Name="textBox1"
           KeyDown="OnKeyDownHandler"/>
  <TextBlock Width="300" Height="100" Name="textBlock1"/>
</StackPanel>

C#:

private void OnKeyDownHandler(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Return)
    {
        textBlock1.Text = "You Entered: " + textBox1.Text;
    }
}

或者也许是一些不同的方法来创建它?

【问题讨论】:

  • 嗯……你想做什么?!因为这不会保存您之前输入的内容...

标签: c# wpf keyboard key detection


【解决方案1】:

你可以直接绑定文本:

<StackPanel>
  <TextBlock Width="300" Height="20">
   Type some text into the TextBox and it will appear in the field automatically.
  </TextBlock>
  <TextBox Width="300" Height="30" Name="textBox1" />
  <TextBlock Width="300" Height="100" Name="textBlock1" Text="{Binding Text, ElementName=textbox1}"/>
</StackPanel>

这样你就不需要任何代码隐藏了。

编辑

如果你想要更复杂的东西,试试这个。在您的项目中实现一个新类,如下所示:

public class MyConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return $"You entered: {value ?? "nothing"}";
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

然后将绑定更改为

<Window.Resources>
    <local:MyConverter x:Key="MyConverter"/>
</Window.Resources>
<StackPanel>
    <TextBox Name="txtEdit" />
    <TextBlock Text="{Binding Text, Converter={StaticResource MyConverter}, ElementName=txtEdit}" />
</StackPanel>

不要忘记窗口的资源。

这是一个显示它的屏幕视频:

【讨论】:

  • 是的,但直到焦点丢失在文本框上才会出现
  • 如果您使用 UpdateSourceTrigger PropertyChanged,它会。回家后我会重新制作样品。
  • 这就是我在催促你做的事情 ;)
  • 好的,我想我想要什么。 ` private void Window_KeyDown(object sender, KeyEventArgs e) { textBlock1.Text = Convert.ToString(e.Key); }`谢谢大家的帮助:D
  • @MickyD 我刚刚意识到 UpdateSourceTrigger 在这里不合适,因为 TextBlock 的 Text-property 不是交互式的,这意味着您不需要它。它可以在我的机器上使用我所做的编辑。
【解决方案2】:
textBlock1.Text = "You Entered: " + **textBox1.Text**;

不要使用直接控制属性,相反使用MVVM和绑定。

“绑定的 UpdateSourceTrigger 属性控制如何以及何时将更改的值发送回源。”

http://www.wpf-tutorial.com/data-binding/the-update-source-trigger-property/

【讨论】:

  • 我认为您走在正确的道路上。您可能想进一步说明,通过使用此特定绑定更新会立即发生,无需输入或制表符
【解决方案3】:

如果我正确理解了这个问题,您需要隧道 PreviewKeyDown 事件:

private void OnPreviewKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.G)
    {
        e.Handled = true;
    }
}

或者,您可以使用Keyboard 类。事实上,Keyboard 类可以在您的代码中任何地方使用:

private void SomeMethod()
{
    if (Keyboard.IsKeyDown(Key.LeftCtrl))
    {
        MessageBox.Show("Release left Ctrl button");
        return;
    }
    //Do other work
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-21
    • 2021-04-21
    • 1970-01-01
    • 2017-07-27
    • 2014-03-31
    • 1970-01-01
    • 1970-01-01
    • 2021-02-28
    相关资源
    最近更新 更多