WPF:在用户控件中设置键盘焦点? (KeyBinding的问题) [英] WPF: Setting Keyboard Focus in a User Control? (Problems with KeyBinding)

查看:283
本文介绍了WPF:在用户控件中设置键盘焦点? (KeyBinding的问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一堆东西的主窗口的应用程序。有时,用户会做出一些响应,我想在主窗口中显示其他内容,暂时隐藏这些内容。



我在做通过使主窗口中最外面的元素成为没有定义行或列的Grid。然后,网格中的每个元素将完全填充网格中的一个单元格,并绘制在其他元素上面。



然后,我常规的一堆东西在网格的第一个元素,而我的临时的东西是一个UserControl作为网格的第二个元素,通常设置为Visibility = Collapsed。

除了KeyBinding之外,工作正常。当在正常的一堆东西中触发适当的命令时,UserControl上的可见性被设置为Visible,并且它完全覆盖了常规的一些东西。当用户单击UserControl上的关闭按钮时,它的可见性再次设置为折叠,然后消失,并显示常规的一堆东西。



我的问题是与KeyBindings。我有几个UserControl上定义 - 不应该在主窗口上定义 - 他们不工作。或者说,他们工作正常,一旦我点击UserControl内,但他们不工作,直到我做。



我需要他们一工作UserControl可见的,而不需要用户点击或制表UserControl正确。

我的猜测是这与键盘焦点有关 - 但我一直无法找到一种方法来设置UserControl的焦点。这里的东西 - UserControl中的唯一元素是一个选项卡控件,其中的所有选项卡都是通过模板动态构建的。在编译时没有元素,我可以明确地引用并传递给KeyBoard.Focus()。

那么,我是否认为它没有重点是造成这个问题?如果是这样,我怎么能把焦点设置到一个TabControl的元素,当我甚至不知道有多少标签,更不用说哪一个是选定的?

我希望这个控件有焦点,当它变得可见时。所以在构造函数中,我在IsVisibleChanged上设置了一个处理程序。

  public MyControl 
{
.. 。
this.IsVisibleChanged + = new DependencyPropertyChangedEventHandler(MyControl_IsVisibileChanged);

$ b $ void MyControl_IsVisibleChanged(object sender,DependencyPropertyChangedEventArgs e)
{
if(!(b.Low) b this.Focusable = true;
Keyboard.Focus(this);
}

我可以在xaml中设置Focusable,但我更喜欢它在代码隐藏中,这样所有的代码都在一个地方。


I have an app that has a main window that contains a bunch of stuff. From time to time the user will do something in response to which I want to display something else entirely in the main window, temporarily hiding what is there.

I'm doing this by making the outermost element in the main window a Grid with no rows or columns defined. Every element in the grid, then will completely fill the one single cell in the grid, drawing on top of the others.

My regular bunch stuff, then, is in the first element of the grid, and my temporary something else is a UserControl as the second element of the grid, that is normally set Visibility=Collapsed.

Except for the KeyBinding everything works fine. When the appropriate command is triggered in the regular bunch of stuff, the visibility on the UserControl is set to Visible, and it covers the regular bunch of stuff completely. When the user clicks on the close button on the UserControl, it's visibility is set to Collapsed, again, and it disappears and the regular bunch of stuff is revealed.

My problem is with KeyBindings. I have a few defined on the UserControl - that should not be defined on the main window - and they don't work. Or rather, they work fine once I click inside the UserControl, but they don't work until I do.

I need them to work as soon as the UserControl is made visible, without requiring the user to click or tab into the UserControl proper.

My guess is that this has something to do with keyboard focus - but I've been unable to find a way to set the focus on the UserControl. Here's the thing - the only element within the UserControl is a tab control, all of the tabs of which are dynamically constructed via templates. There are no elements known at compile time that I can reference explicitly and pass to KeyBoard.Focus().

So, am I right in thinking that it's lack of focus that is causing the problem? And if so, how can I set focus to an element in a TabControl, when I don't even know how many tabs there are, let alone which is the selected one?

解决方案

I wanted this control to have the focus, when it became visible. So in the constructor, I set up a handler on IsVisibleChanged.

public MyControl
{
    ...
    this.IsVisibleChanged += new DependencyPropertyChangedEventHandler(MyControl_IsVisibileChanged);
}

void MyControl_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
    if (!(bool(e.NewValue)
        return;
    this.Focusable = true;
    Keyboard.Focus(this);
}

I could have set Focusable in the xaml, but I prefer it in the code-behind, so that all of the relevant code is in one place.

这篇关于WPF:在用户控件中设置键盘焦点? (KeyBinding的问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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