WPF 中的自定义命令允许“实时”更改依赖属性值
我有一个场景,在我的自定义控件中,我有一个名为 MinValue
的自定义依赖属性,类型为 int
。
我想允许用户通过键盘输入 Ctrl++(增加)和 Ctrl+-(减少)来更改该值。我知道这可以通过命令来完成,但我不知道在哪里实施这些命令。 用户应该能够使用前面提到的键盘快捷键无论窗口中的哪个控件具有焦点。
我认为我必须在自定义控件中实现该命令,因为只有该控件知道如何处理键盘快捷键,但由于我有一个充满自定义控件的列表框,并且每个控件都应该处理快捷键,所以我不确定它是如何工作的。 有什么指点吗? 一如既往的感谢!
I have a scenario where in my custom control I have a custom Dependency Property called MinValue
of type int
.
I want to allow the user to change that value via keyboard input Ctrl++ (increase) and Ctrl+- (decrease). I undertand this can be done with Commands, but am clueless as to where to implement those Commands.
The user should be able to use the beforementioned keyboard shortcuts no matter what control in the Window has focus.
I think I would have to implement the Command in my custom control because only that control knows what to do with the keyboard shortcut, but since I have a listbox full of custom controls, and each should handle the shortcut, I'm not sure how that would work.
Any pointers?
Thanks as always!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
还有其他方法可以做到这一点,并且可能有充分的理由不这样做,但这里有一个将命令放在用户控件上的实现:
控件代码(可能很容易成为无外观的控件):
用户 如果需要行为,需要为这些命令添加 CommandBinding。
窗口代码背后:
有帮助吗?
(哦,顺便说一句 - 正如所写的,此代码仅响应我键盘上的 +/- 键,而不响应 (P)([)(]) 键上方的键 - 我以前没有使用过按键手势。当然可以不必太纠正。)
There are other ways to do this and probably good reasons to not do it this way, but here's an implementation that puts the commands on the user control:
User control code (could just as easily be a lookless control):
The windows on which this behavior is desired need to add a CommandBinding for these commands.
Window code behind:
Does that help?
(Oh, BTW - as written this code only responds to the +/- keys on my keypad, not the ones above the (P)([)(]) keys - I haven't worked with key gestures before. Surely it can't be very had to correct.)