我想允许用户使用箭头键在控件中导航。
用户应该仍然能够使用 Tab 和 Shift + Tab 控件进行水平导航,但我希望他们能够垂直导航(如果他们水平导航,这可能会跳过将聚焦的控件)。
如果我对似乎发生的事情使用该MoveFocus
方法,则会跳过某些控件,例如按钮和可编辑的组合框。UIElement
有人知道为什么吗?这些控件通常使用 TAB 聚焦,但 Up/Down/Next 的 FocusDirections 似乎跳过了这些控件。如果我看一下,PredictFocus
它似乎报告说按钮应该以这种方式聚焦,而不是可编辑的组合框。
下面的演示代码:XAML:
<Window x:Class="Focus.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Width="525"
Height="350">
<Window.Resources>
<Style x:Key="FocusStyle">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Rectangle Stroke="CadetBlue" StrokeThickness="2" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="txtBox1" Width="75"/>
<TextBox Width="200"
Name="txtBox1"
Margin="5"
HorizontalAlignment="Left"
VerticalAlignment="Top"
FocusVisualStyle="{StaticResource FocusStyle}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="button1" Width="75" />
<Button Width="200"
Name="button1"
Height="25"
Margin="5"
Content="Hello"
HorizontalAlignment="Left"
VerticalAlignment="Top"
FocusVisualStyle="{StaticResource FocusStyle}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="txtBox2" Width="75"/>
<TextBox Width="200"
Name="text2"
Margin="5"
HorizontalAlignment="Left"
VerticalAlignment="Top"
FocusVisualStyle="{StaticResource FocusStyle}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="comboBox" Width="75"/>
<ComboBox Width="200"
Margin="5"
Name="comboBox"
HorizontalAlignment="Left"
VerticalAlignment="Top"
FocusVisualStyle="{StaticResource FocusStyle}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="txtBox4" Width="75"/>
<TextBox Width="200"
Margin="5"
Name="txtBox4"
HorizontalAlignment="Left"
VerticalAlignment="Top"
FocusVisualStyle="{StaticResource FocusStyle}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="comboBox2" Width="75"/>
<ComboBox Width="200"
Margin="5"
IsEditable="True"
Name="comboBox2"
HorizontalAlignment="Left"
VerticalAlignment="Top"
FocusVisualStyle="{StaticResource FocusStyle}" />
</StackPanel>
<TextBlock Margin="3" Text="{Binding FocussedControl.Name, StringFormat=Focused Control: {0}}" />
<TextBlock Margin="3" Text="{Binding PredictedFocusControl.Name, StringFormat=Predicted Focus {0}}"/>
</StackPanel>
</Grid>
</Window>
MainWindow.Xaml.cs:
using System.Windows;
using System.Windows.Input;
namespace Focus
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
public IInputElement FocussedControl
{
get { return (IInputElement)GetValue(FocussedControlProperty); }
set { SetValue(FocussedControlProperty, value); }
}
public static readonly DependencyProperty FocussedControlProperty =
DependencyProperty.Register("FocussedControl", typeof(IInputElement), typeof(MainWindow));
public DependencyObject PredictedFocusControl
{
get { return (DependencyObject)GetValue(PredictedFocusControlProperty); }
set { SetValue(PredictedFocusControlProperty, value); }
}
public static readonly DependencyProperty PredictedFocusControlProperty =
DependencyProperty.Register("PredictedFocusControl", typeof(DependencyObject), typeof(MainWindow));
protected override void OnPreviewLostKeyboardFocus(KeyboardFocusChangedEventArgs e)
{
FocussedControl = e.NewFocus;
}
protected override void OnPreviewKeyDown(KeyEventArgs e)
{
base.OnPreviewKeyDown(e);
if (e.Key == Key.Down)
{
var success = (FocussedControl as UIElement).MoveFocus(new TraversalRequest(FocusNavigationDirection.Down));
System.Diagnostics.Debug.Assert(success);
PredictedFocusControl = (FocussedControl as UIElement).PredictFocus(FocusNavigationDirection.Down);
}
else if (e.Key == Key.Up)
{
var success = (FocussedControl as UIElement).MoveFocus(new TraversalRequest(FocusNavigationDirection.Up));
PredictedFocusControl = (FocussedControl as UIElement).PredictFocus(FocusNavigationDirection.Up);
}
}
}
}