In wpf, how can I get the following control in tab order

That's it, I know how to set focus on the next control in tab order, but I really don't want to change focus ... I just want to get the next control in tab order (maybe get the previous, first and last). So ... howyadodat?

M

+6
source share
3 answers

I don’t think it’s possible to have the following control in tab order, but you can loop into collections of children and use KeyboardNavigation.GetIsTabStop(..) KeyboardNavigation.GetTabIndex() to create an assistant for thet.

0
source

PredictFocus was supposed to get Tab support in .NET 4, but the current MSDN implies that it is not. However, this may be overseeing the documentation; I have not tried, but you could do it.

If this does not work, there is a private method on KeyboardNavigation that may be useful to you; you will have to use reflection to invoke it, and for this you will need the appropriate access rights to the code, but this may work .. The NET Reflector shows the signature as follows:

 private DependencyObject GetNextTab(DependencyObject e, DependencyObject container, bool goDownOnly) 

Where e is the element for which you want to get the next tab, and container is its parent container. I'm not 100% sure what goDownOnly does, but I think that means you don't want to leave the parent container. The method will return null if there is no next tab for this element.

Keep in mind that this is a private method; very susceptible to change, the next version will come.

Edit: You will need an instance of KeyboardNavigation ! I completely forgot about it. There is static in FrameworkElement.KeyboardNavigation , but it is also internal, so reflection needs to be obtained.

0
source

PredictFocus(FocusNavigationDirection.Next) does not work as @Cameron said. My shell code based on @Randolpho post now works well.

I tried several templates and finally came to the conclusion that I have to make sure that container is actually one of the parents of e to avoid unexpected results.

 /// <summary> /// Get next tab order element. /// </summary> /// <param name="e">The element to get next tab order</param> /// <param name="container">The container element owning 'e'. Make sure this is a container of 'e'.</param> /// <param name="goDownOnly">True if search only itself and inside of 'container'; otherwise false. /// If true and next tab order element is outside of 'container', result in null.</param> /// <returns>Next tab order element or null if not found</returns> public DependencyObject GetNextTab(DependencyObject e, DependencyObject container, bool goDownOnly) { var navigation = typeof(FrameworkElement) .GetProperty("KeyboardNavigation", BindingFlags.NonPublic | BindingFlags.Static) .GetValue(null); var method = navigation .GetType() .GetMethod("GetNextTab", BindingFlags.NonPublic | BindingFlags.Instance); return method.Invoke(navigation, new object[] { e, container, goDownOnly }) as DependencyObject; } 

Out.)

 var nextElement = GetNextTab(textbox1, window, false); 
0
source

Source: https://habr.com/ru/post/886480/


All Articles