如何通过按下向下箭头将焦点从tabitem标题移动到此tabitem的内容? 我尝试使用KeyboardNavigation但是当我按下或向上键时,Keyboardfocus仍然没有移动。
提前致谢。
答案 0 :(得分:1)
我创建了一个附加属性来解决类似的问题。当您按ContentControl(TabItem)上的某个键时,它会关注内容。 XAML看起来像这样
<TabControl Focusable="False">
<TabItem Header="Main" local:Focus.ContentOn="Down">
<Stackpanel>
<TextBox />
</Stackpanel>
</TabItem>
附属物:
public static class Focus
{
public static Key GetContentOn(DependencyObject obj)
{
return (Key)obj.GetValue(ContentOnProperty);
}
public static void SetContentOn(DependencyObject obj, Key value)
{
obj.SetValue(ContentOnProperty, value);
}
// Using a DependencyProperty as the backing store for ContentOn. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ContentOnProperty =
DependencyProperty.RegisterAttached("ContentOn", typeof(Key), typeof(Navigate),
new FrameworkPropertyMetadata(Key.None, OnContentOnChanged));
private static void OnContentOnChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
var control = o as ContentControl;
if (control != null)
control.KeyDown += FocusContent;
}
private static void FocusContent(object sender, KeyEventArgs e)
{
if (Keyboard.FocusedElement == sender)
{
var control = sender as ContentControl;
if (control != null && control.HasContent && GetContentOn(control) == e.Key)
{
((FrameworkElement)control.Content).MoveFocus(new TraversalRequest(FocusNavigationDirection.First));
e.Handled = true;
}
}
}
}