创建新选项卡时,WPF将焦点放在选项卡控件内容中 [英] WPF Focus In Tab Control Content When New Tab is Created

查看:202
本文介绍了创建新选项卡时,WPF将焦点放在选项卡控件内容中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个MainView (窗口)包含一个选项卡控件。选项卡控件绑定到ChildViews(用户控件)的ObservableCollection。 MainView的ViewModel有一个方法,允许添加到ChildViews的集合,然后创建一个新的选项卡。当一个新的选项卡被创建,它成为活动选项卡,这工作正常。这个在MainView上的方法是从另一个ViewModel(OtherViewModel)中调用的。

我要做的是将键盘焦点设置为选项卡上的第一个控件(一个AutoCompleteBox从WPFToolkit *)当一个新的选项卡创建。我也需要以相同的方式设置焦点,但没有创建一个新的标签(所以把焦点设置在当前活动的标签)。

是AutoCompleteBox的一些焦点问题 - 即使它确实有焦点,你需要发送一个MoveNext()到它的窗口来获取光标,我已经解决了这个问题。)



所以这是问题。当我没有创建一个新的标签时,对焦工作,但是当我创建一个新的标签时它不工作。两个函数都使用相同的方法来设置焦点,但创建逻辑首先调用创建新选项卡并将其设置为活动的方法。设置焦点的代码(在ChildView的Codebehind中):

  IInputElement element1 = Keyboard.Focus(autoCompleteBox); 
//加上代码来处理AutoCompleteBox,如上所述。

无论哪种情况,Keyboard.FocusedElement都以MainView开始。创建后,调用Keyboard.Focus似乎什么都不做(聚焦的元素仍然是MainView)。在没有创建标签的情况下正确调用它,将键盘焦点设置为autoCompleteBox。



有什么想法?

更新:

本德尔的建议是半工半工。

所以现在在这两种情况下,元素正确的是AutoCompleteBox。然后我做的是MoveNext(),它将焦点设置为一个TextBox。我一直假定这个文本框是内部的自动完成框,因为焦点正确地设置在屏幕上,当发生这种情况。现在我不太确定。这仍然是我看到这个代码时,没有做一个创建的行为。在创建之后,MoveNext()将焦点设置回到MainView中的元素。



问题仍然必须符合Bender的答案,控件是不一样的取决于是否创建一个新的选项卡。任何其他的想法?

最终更新

如前所述,majocha的建议工作。


我想更新这个以防万一任何人遇到与AutoCompleteBox相同的问题。看来设置焦点不会在用户界面中激活它 - 您需要对其执行一次MoveNext以将焦点向前移动到控件的内部文本框。这是基于我的调试经验,这可能不是100%科学的。如果我有时间,我会尝试创建一个小的repro项目,并将其提交给WPFToolkit团队。 解决方案

您可以尝试推迟焦点更改

  Dispatcher.BeginInvoke(MyChangeFocusAction,DispatcherPriority.ContextIdle); 

布局和属性更新完成后,它将排队。
我不认为这是最好的做法,但它对我有用。


I've done a lot of searching on SO and google around this problem, but can't seem to find anything else to try.

I have a MainView (window) that contains a tab control. The tab control binds to an ObservableCollection of ChildViews (user controls). The MainView's ViewModel has a method that allows adding to the collection of ChildViews, which then creates a new tab. When a new tab is created, it becomes the active tab, and this works fine. This method on the MainView is called from another ViewModel (OtherViewModel).

What I am trying to do is set the keyboard focus to the first control on the tab (an AutoCompleteBox from WPFToolkit*) when a new tab is created. I also need to set the focus the same way, but WITHOUT creating a new tab (so set the focus on the currently active tab).

(*Note that there seem to be some focus problems with the AutoCompleteBox--even if it does have focus you need to send a MoveNext() to it to get the cursor in its window. I have worked around this already).

So here's the problem. The focusing works when I don't create a new tab, but it doesn't work when I do create a new tab. Both functions use the same method to set focus, but the create logic first calls the method that creates a new tab and sets it to active. Code that sets the focus (in the ChildView's Codebehind):

        IInputElement element1 = Keyboard.Focus(autoCompleteBox);
        //plus code to deal with AutoCompleteBox as noted.

In either case, the Keyboard.FocusedElement starts out as the MainView. After a create, calling Keyboard.Focus seems to do nothing (focused element is still the MainView). Calling this without creating a tab correctly sets the keyboard focus to autoCompleteBox.

Any ideas?

Update:

Bender's suggestion half-worked.

So now in both cases, the focused element is correctly the AutoCompleteBox. What I then do is MoveNext(), which sets the focus to a TextBox. I have been assuming that this Textbox is internal to the AutoCompleteBox, as the focus was correctly set on screen when this happened. Now I'm not so sure. This is still the behavior I see when this code gets hit when NOT doing a create. After a create, MoveNext() sets the focus to an element back in my MainView.

The problem must still be along the lines of Bender's answer, where the state of the controls is not the same depending on whether a new tab was created or not. Any other thoughts?

Final Update

As noted, majocha's suggestion worked.

I wanted to update this in case anyone happened upon this same problem with the AutoCompleteBox. It appears that setting focus does not activate it in the UI--you need to do a MoveNext on it to move focus forward once to the control's internal Textbox. This is based on my debugging experience, which may not be 100% scientific. If I have time, I will attempt to create a small repro project and submit it to the WPFToolkit team.

解决方案

You can try defering the focus change with

Dispatcher.BeginInvoke(MyChangeFocusAction, DispatcherPriority.ContextIdle);

It will get queued after layout and properties updates are done. I don't think it's best practice, but it works for me.

这篇关于创建新选项卡时,WPF将焦点放在选项卡控件内容中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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