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

查看:112
本文介绍了如何将焦点设置到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.

我的控件包含:
-工具栏带有绑定到相应命令的多个按钮 虚拟机
-TextBox,它是过滤器的输入
-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问题清楚地显示了它我相信.

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天全站免登陆