Мне нужно было сделать нечто подобное в проекте, над которым я работаю, и я нашел что-то, что, кажется, работает достаточно хорошо.
Вот небольшой демонстрационный проект с кодом:
XAML:
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication3"
Title="MainWindow" SizeToContent="WidthAndHeight">
<StackPanel>
<TextBox Width="200" />
<TextBox Width="200" />
<TextBox Width="200" />
</StackPanel>
</Window>
Код сзади:
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
namespace WpfApplication3
{
public partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
// Code needs window to be active to work, so just call it in Loaded event for demo
this.Loaded += (s, e) =>
{
FocusManager.SetFocusedElement(this, this);
UIElement element = FocusManager.GetFocusedElement(this) as UIElement;
element.MoveFocus(new TraversalRequest(FocusNavigationDirection.First));
};
}
}
}
Я знаю, что это действительно поздний ответ, но поможет ли это вам вообще?