如何将焦点设置为 UserControl(使其可选择)? [英] How to set focus to UserControl (make it selectable)?

查看:24
本文介绍了如何将焦点设置为 UserControl(使其可选择)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将焦点设置到 UserControl 本身,而不是它的子级.
否则我无法实现从缓冲区插入.:(
设置 Focusable=True 没有帮助.Google 和 SO 仅说明如何将焦点设置到 UserControl 子级.

I need to set focus to UserControl itself, not its child.
Otherwise I cannot implement insertion from the buffer. :(
Setting Focusable=True doesn't help. Google and SO tells only how to set focus to UserControl child.

我的控件包含:
- 工具栏有几个按钮绑定到相应的命令虚拟机
- 作为过滤器输入的文本框
- DataGrid - 项目列表.

My control contains:
- Toolbar with several buttons bound to commands of the corresponding VM
- TextBox which is the input for the filter
- DataGrid - list of items.

我需要将 Ctrl+V 命令绑定到 VM.但是要处理这个手势 UserControl 必须有焦点.当网格中没有项目(VM 的集合为空)时,按钮被禁用,唯一可以获得焦点的元素是 TextBox.但它以自己的方式处理 Ctrl+V,我不想改变这种行为.
因此,当我单击 UserControl 区域时,我需要一些东西来设置焦点.
我相信 UserControl 是最好的选择.
但我不知道如何让它可选.

I need to bind Ctrl+V command to VM. But to handle this gesture UserControl must have focus within. When there are no items in the grid (VM's collection is empty) buttons are disabled and the only element which can get focus is TextBox. But it handles Ctrl+V in its own way and I don't want to change this behavior.
Thus, I need something to set focus to when I click the area of UserControl.
I believe UserControl is the best candidate for it.
But I don't know how to make it selectable.

推荐答案

整个问题在于我对控件行为的误解.
这个 SO question 清楚地表明了这一点我相信.

The whole problem was in my misunderstanding of controls' behavior.
This SO question clearly shows it I believe.

因此,设置 UserControl.Focusable = true 是不够的.要使其可通过键盘导航,IsTabStop 也必须为 true.为了使 UC 可以通过鼠标单击选择,我们应该在鼠标事件处理程序中调用 Focus().就是这样.

Thus, setting UserControl.Focusable = true is not sufficient. To make it navigatable via keyboard IsTabStop must be true also. And to make UC selectable by mouse click we should call Focus() in mouse eventhandler. That's it.

这篇关于如何将焦点设置为 UserControl(使其可选择)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆