사용자가 화살표 키를 사용하여 컨트롤 내에서 탐색 할 수있게하려고합니다.MoveFocus가 컨트롤을 건너 뜁니다.
사용자는 Tab 키와 Shift + Tab 컨트롤을 사용하여 가로로 탐색 할 수 있어야하지만 세로로 탐색 할 수 있어야합니다 (가로로 탐색 할 경우 집중되는 컨트롤을 건너 뛸 수 있음).
UIElement
에서 MoveFocus
메서드를 사용하면 어떤 컨트롤이 단추 및 편집 가능한 콤보 상자와 같이 건너 뛴다는 것이 있습니다.
이유가 누구인지 아시겠습니까? 이러한 컨트롤은 TAB을 사용하여 일반적으로 초점이 맞춰 지지만 Up/Down/Next의 FocusDirections는 컨트롤을 건너 뛰는 것처럼 보입니다. PredictFocus
을 살펴보면 Buttons는 이러한 방식으로 포커스를 설정할 수 있지만 편집 가능한 콤보 상자는 표시 할 수 없다고보고됩니다. 아래
데모 코드 : 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);
}
}
}
}
이 버튼에 문제가 PreviewKeyDown 이벤트를 처리하지 않은 것으로 보인다. 그러나 편집 가능한 콤보 상자는 여전히 포커스를 얻을 수 없습니다. –