如何过滤键盘数字键
我希望聚焦特定的 Textbox
并仅在用户按下数字键(1、2、3 等)时接受按键,否则我不想聚焦该 Textbox< /code>...
Senario:
我在视图中有 ListView
(具有自定义视图)。在列表视图下方我有一个文本框。
假设 ListView
包含编号为 1、2、3、33、373 等的项目。
现在,当我按下数字键(例如 Key 3
)时,应该发生以下操作:
- 聚焦特定的
TextBox
- 附加带有数字输入的
TextBox
文本 - 选择
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:
- Focus that Specific
TextBox
- Append the
TextBox
text with the number input - Select the item in the
ListView
with the same number asTextBox.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个逻辑是特定于视图的。如果你把它放在代码后面就可以了。 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
如果您使用 MVVM,那么附加行为就是正确的选择... http://eladm.wordpress.com/2009/04/02/attached-behavior/
将按键源元素命名为
ListView
...声明并定义
ListView
类型的附加属性,例如NumericKeyPressBehavior .FocusTarget
此来源
FocusTarget
将是您的MyListView
并且该行为将附加到您的TextBox
在 NumericKeyPressBehavior.FocusTarget 的依赖属性更改事件处理程序中,处理 ListView 上的按键,然后根据是否按下数字键,聚焦目标 TextBox 并将按下的按键字符附加到它。
GetKeyCharFromKeyCode
位于此处....C#如何将虚拟键码转换为字符?这有帮助吗?
If you are using MVVM then attached behavior is the way to go ... http://eladm.wordpress.com/2009/04/02/attached-behavior/
Name you key press source element like
ListView
...Declare and define an attached property of type
ListView
sayNumericKeyPressBehavior.FocusTarget
The Source for this
FocusTarget
will be youMyListView
and the behavior will be attached to yourTextBox
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.GetKeyCharFromKeyCode
is found here.... C# How to translate virtual keycode to char?Does this help?