如何过滤键盘数字键

发布于 2024-12-08 10:21:09 字数 1046 浏览 2 评论 0原文

我希望聚焦特定的 Textbox 并仅在用户按下数字键(1、2、3 等)时接受按键,否则我不想聚焦该 Textbox< /code>...

Senario:

我在视图中有 ListView (具有自定义视图)。在列表视图下方我有一个文本框。

假设 ListView 包含编号为 1、2、3、33、373 等的项目。

现在,当我按下数字键(例如 Key 3)时,应该发生以下操作:

  1. 聚焦特定的 TextBox
  2. 附加带有数字输入的 TextBox 文本
  3. 选择 ListView 中与 TextBox 具有相同数字的项目。文本

我尝试过的 Xaml

<ListView Name="lv"
              Grid.Row="1"
              ItemsSource="{Binding}"
              View="{Binding Path=SelectedItem,
                             ElementName=viewComboBox}" DisplayMemberPath="Name" IsTextSearchEnabled="True" TextSearch.TextPath="{Binding Path=Person.Name}"/>
    <TextBox  Grid.Row="2" Text="{Binding Path=TextSearch.Text,ElementName=lv}"></TextBox>  

TextBox 中没有显示任何内容,并且我不知道如何处理数字按键。

我需要这个,并且使用 MVVM 令人困惑......

在这方面的任何帮助都会很棒。使用代码进行一些指导会更好...谢谢...

I wish to focus a specific Textbox and accept the key press only when the user presses a number key (1,2,3 etc.) otherwise I don't want to focus that Textbox...

Senario:

I have ListView (having a custom view) in a view. below the list view i have a Textbox.

Lets say ListView contains items numbered as 1, 2, 3, 33, 373 etc.

Now when I press a number key, lets say Key 3, the following action should occur:

  1. Focus that Specific TextBox
  2. Append the TextBox text with the number input
  3. Select the item in the ListView with the same number as TextBox.Text has

My Xaml for what I tried

<ListView Name="lv"
              Grid.Row="1"
              ItemsSource="{Binding}"
              View="{Binding Path=SelectedItem,
                             ElementName=viewComboBox}" DisplayMemberPath="Name" IsTextSearchEnabled="True" TextSearch.TextPath="{Binding Path=Person.Name}"/>
    <TextBox  Grid.Row="2" Text="{Binding Path=TextSearch.Text,ElementName=lv}"></TextBox>  

Nothing is displayed in the TextBox and I don't know how to handle numeric key press.

I need this and its bewildering using MVVM...

Any help in this regard would be great. And some guidance using code would be even better... Thanks...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浊酒尽余欢 2024-12-15 10:21:09

这个逻辑是特定于视图的。如果你把它放在代码后面就可以了。 MVVM 不会阻止您在代码隐藏中编写视图特定代码。

但是,如果您虔诚地遵循“无代码隐藏”方法,那么您可以创建一个 行为 并将所有里面的代码。您的行为将附加到列表框,并将文本框引用作为属性。它将侦听列表框上的 keydown 事件并将键添加到文本框的文本属性。

你不应该在 ViewModel 中有这样的逻辑

This logic is specific to view. Its fine if you put it in code behind. MVVM doesn't stop you from writing view specific code in code behind.

However if your religiously follow the 'no code behind' approach then you can create a Behavior and put all the code in that. Your behavior will be attached to listbox and will take textbox reference as property. It will listen to keydown event on listbox and add keys to textbox's text property.

You shouldn't really be having logic like this in ViewModel

牛↙奶布丁 2024-12-15 10:21:09

如果您使用 MVVM,那么附加行为就是正确的选择... http://eladm.wordpress.com/2009/04/02/attached-behavior/

  1. 将按键源元素命名为ListView ...

    ;
       ....
    
    
  2. 声明并定义 ListView 类型的附加属性,例如 NumericKeyPressBehavior .FocusTarget

  3. 此来源FocusTarget 将是您的 MyListView 并且该行为将附加到您的 TextBox

     >
    
  4. 在 NumericKeyPressBehavior.FocusTarget 的依赖属性更改事件处理程序中,处理 ListView 上的按键,然后根据是否按下数字键,聚焦目标 TextBox 并将按下的按键字符附加到它。

    private static void OnFocusTargetChanged(
        依赖对象 o, 
        DependencyPropertyChangedEventArgs e)
    {
        var textBox = o 作为文本框;
        var listView = e.NewValue 作为 ListView;
        if (textBox != null && listView != null)
        {
            listView.KeyUp += 
             (o1, e1) =>
                {
                    var keyChar = GetKeyCharFromKeyCode(e1.Key);
                    if ("0123456789".Contains(keyChar.ToString()))
                    {
                        文本框.Focus();
                        textBox.Text += keyChar.ToString();
                    }
                }
        }
    }
    

GetKeyCharFromKeyCode 位于此处....C#如何将虚拟键码转换为字符?

这有帮助吗?

If you are using MVVM then attached behavior is the way to go ... http://eladm.wordpress.com/2009/04/02/attached-behavior/

  1. Name you key press source element like ListView ...

    <ListView x:Name="MyListView">
       ....
    </ListView>
    
  2. Declare and define an attached property of type ListView say NumericKeyPressBehavior.FocusTarget

  3. The Source for this FocusTarget will be you MyListView and the behavior will be attached to your TextBox

     <TextBox local:NumericKeyPressBehavior.FocusTarget="{Binding ElementName=MyListView}" 
              .... />
    
  4. In NumericKeyPressBehavior.FocusTarget's dependency property changed event handler, handle the key press on the ListView and then based on if a numeric key was pressed, focus the target TextBox and also append the pressed key character to it.

    private static void  OnFocusTargetChanged(
        DependencyObject o, 
        DependencyPropertyChangedEventArgs e)
    {
        var textBox = o as TextBox;
        var listView = e.NewValue as ListView;
        if (textBox != null && listView != null)
        {
            listView.KeyUp += 
             (o1, e1) =>
                {
                    var keyChar = GetKeyCharFromKeyCode(e1.Key);
                    if ("0123456789".Contains(keyChar.ToString()))
                    {
                        textBox.Focus();
                        textBox.Text += keyChar.ToString();
                    }
                }
        }
    }
    

GetKeyCharFromKeyCode is found here.... C# How to translate virtual keycode to char?

Does this help?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文