【问题标题】:how to create an Enter event for textbox in wpf xaml code? [closed]如何在 wpf xaml 代码中为文本框创建 Enter 事件? [关闭]
【发布时间】:2013-03-20 12:04:56
【问题描述】:

我刚接触 wpf,最近 3 天开始学习 xaml,当我在 xaml 代码中为我的文本框创建输入事件时,我继续创建 y?如何创建进入 evnet?一些事件没有问题,例如点击、文本更改事件。

【问题讨论】:

    标签: wpf xaml enter


    【解决方案1】:

    WPF 文本框中没有“Enter”事件。 您应该考虑其他事件,例如: 有焦点, 鼠标输入, 鼠标按下, GotKeybordFocus 看你需要什么。

    在 xaml 中,您可以这样写:<TextBox Height="23" Canvas.Left="374" TextWrapping="Wrap" Text="TextBox" Canvas.Top="272" Width="120" GotFocus="TextBox_GotFocus_1"/>

    但使用设计器更容易。 属性->事件->双击需要生成的事件进行控制。

    【讨论】:

      【解决方案2】:

      只有当焦点在 TextBox 中时用户按 Enter 时才需要它?

      如果您的设计是带有命令的 MVVM,您可以做什么:

      与Window.Resources同级:

      <Window.InputBindings>
          <KeyBinding Key="Enter" Command="{Binding EnterCommand}"/>
      </Window.InputBindings>
      

      但这会在你的窗口中的任何地方捕捉到 Enter 键,而不仅仅是当你关注你的文本框时......

      【讨论】:

      • 感谢您的回复,我想像普通的 Windows 文本框一样为文本框创建输入事件。目的是每当我进入文本框时选择文本。像普通的win app textbox.selectall()
      【解决方案3】:

      您可以使用TextChanged 事件和处理程序的代码,

      private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
              {
      
              }
      

      【讨论】:

        猜你喜欢
        • 2020-09-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多