English 中文(简体)
如何过滤键盘号
原标题:How can I filter keyboard number keys

我愿着重谈谈具体的<代码>Text Box,并只有在用户发布一些关键(1,2,3等)时,才接受关键媒体。

Senario:

我有<代码>ListView(反映一种习俗观点)。 名单见Text Box

请允许我说,载有编号为1、2、3、33、373等的项目。

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>  

<代码>TextBox没有显示任何情况,我不知道如何处理数字关键新闻。

I need this and its bewildering using MVVM...

在这方面,任何帮助都是巨大的。 使用守则的某些指导甚至更好...... ......

问题回答

这种逻辑是特别的。 如果你将它放在法典上的话,它会受到罚款。 跨国激进党没有阻止你在后面的法典中看到具体守则。

然而,如果你在宗教上不遵循该做法背后的守则,你可以创建Behavior,并将该守则列入其中。 你们的行为将附在清单箱中,并作为财产使用文字箱。 它将听取名单框上的关键性活动,并在文本框中添加关键内容。

你的逻辑确实与这一点一样。

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?





相关问题
WPF convert 2d mouse click into 3d space

I have several geometry meshes in my Viewport3D, these have bounds of (w:1800, h:500, d:25). When a user clicks in the middle of the mesh, I want the Point3D of (900, 500, 25)... How can I achieve ...

Editing a xaml icons or images

Is it possible to edit a xaml icons or images in the expression design or using other tools? Is it possible to import a xaml images (that e.g you have exported) in the expression designer for editing?...

WPF: writing smoke tests using ViewModels

I am considering to write smoke tests for our WPF application. The question that I am faced is: should we use UI automation( or some other technology that creates a UI script), or is it good enough to ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

How do WPF Markup Extensions raise compile errors?

Certain markup extensions raise compile errors. For example StaticExtension (x:Static) raises a compile error if the referenced class cannot be found. Anyone know the mechanism for this? Is it baked ...

WPF design-time context menu

I am trying to create a custom wpf control, I m wondering how I can add some design-time features. I ve googled and can t seem to get to my goal. So here s my simple question, how can I add an entry ...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签