2012-07-11 4 views
1

사용자가 화살표 키를 사용하여 컨트롤 내에서 탐색 할 수있게하려고합니다.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); 
      } 
     } 
    } 
} 
+0

이 버튼에 문제가 PreviewKeyDown 이벤트를 처리하지 않은 것으로 보인다. 그러나 편집 가능한 콤보 상자는 여전히 포커스를 얻을 수 없습니다. –

답변

0

나는 탭 컨트롤에 집중하기 위해 노력에도 불구하고 콤보 상자에서 IsTabStop = true를 설정하여 고정 :

<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 Width="75" Content="txtBox1" /> 
       <TextBox Name="txtBox1" 
         Width="200" 
         Margin="5" 
         HorizontalAlignment="Left" 
         VerticalAlignment="Top" 
         FocusVisualStyle="{StaticResource FocusStyle}" /> 
       <TextBox Width="200" 
         Margin="5" 
         HorizontalAlignment="Left" 
         VerticalAlignment="Top" 
         FocusVisualStyle="{StaticResource FocusStyle}" /> 
      </StackPanel> 
      <StackPanel Orientation="Horizontal"> 
       <Label Width="75" Content="button1" /> 
       <Button Name="button1" 
         Width="200" 
         Height="25" 
         Margin="5" 
         HorizontalAlignment="Left" 
         FocusVisualStyle="{StaticResource FocusStyle}" 
         VerticalAlignment="Top" 
         Content="Hello" /> 
       <TextBox Width="200" 
         Margin="5" 
         HorizontalAlignment="Left" 
         VerticalAlignment="Top" 
         FocusVisualStyle="{StaticResource FocusStyle}" /> 
      </StackPanel> 
      <StackPanel Orientation="Horizontal"> 
       <Label Width="75" Content="txtBox2" /> 
       <TextBox Name="text2" 
         Width="200" 
         Margin="5" 
         HorizontalAlignment="Left" 
         FocusVisualStyle="{StaticResource FocusStyle}" 
         VerticalAlignment="Top" /> 
       <TextBox Width="200" 
         Margin="5" 
         HorizontalAlignment="Left" 
         VerticalAlignment="Top" 
         FocusVisualStyle="{StaticResource FocusStyle}" /> 
      </StackPanel> 
      <StackPanel Orientation="Horizontal"> 
       <Label Width="75" Content="comboBox" /> 
       <ComboBox Name="comboBox" 
          Width="200" 
          Margin="5" 
          HorizontalAlignment="Left" 
          VerticalAlignment="Top" 
          FocusVisualStyle="{StaticResource FocusStyle}"> 
        <ComboBoxItem Content="Item1" /> 
        <ComboBoxItem Content="Item2" /> 
        <ComboBoxItem Content="Item3" /> 
        <ComboBoxItem Content="Item4" /> 
       </ComboBox> 
       <TextBox Width="200" 
         Margin="5" 
         HorizontalAlignment="Left" 
         VerticalAlignment="Top" 
         FocusVisualStyle="{StaticResource FocusStyle}" /> 
      </StackPanel> 
      <StackPanel Orientation="Horizontal"> 
       <Label Width="75" Content="txtBox4" /> 
       <TextBox Name="txtBox4" 
         Width="200" 
         Margin="5" 
         HorizontalAlignment="Left" 
         VerticalAlignment="Top" 
         FocusVisualStyle="{StaticResource FocusStyle}" /> 
       <TextBox Width="200" 
         Margin="5" 
         HorizontalAlignment="Left" 
         VerticalAlignment="Top" 
         FocusVisualStyle="{StaticResource FocusStyle}" /> 
      </StackPanel> 
      <StackPanel Orientation="Horizontal" > 
       <Label Width="75" Content="comboBox2" /> 
       <ComboBox Name="comboBox2" 
          Width="200" 
          Margin="5" 
          HorizontalAlignment="Left" 
          VerticalAlignment="Top" 
          IsTabStop="True" 
          FocusVisualStyle="{StaticResource FocusStyle}" 
          IsEditable="True" /> 
       <TextBox Width="200" 
         Margin="5" 
         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> 
1

나는 지금은 작동 FocusManager.IsFocusScope="True" Focusable="True"으로이 palying하여 문제를 해결 :

<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" Focusable="True" FocusManager.IsFocusScope="True"> 
      <Label Content="button1" Width="75" /> 
      <Button Width="200" Focusable="True" IsTabStop="True" 
        Name="button1" FocusManager.IsFocusScope="True" 
        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" Focusable="True" 
        Margin="5" IsTabStop="True" 
        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" FocusManager.IsFocusScope="True" Focusable="True"> 
      <Label Content="comboBox2" Width="75"/> 
      <ComboBox Width="200" Focusable="True" 
        Margin="5" IsTabStop="True" 
         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> 

왜 그런지 말할 수 없어요 ...

+0

FocusScope이나 StackPanel에 포커스를 맞추는 것이 중요하지 않습니다. OnPreviewKeyDown 메서드에 e.Handled = true를 추가하면 버튼에 포커스가 발생합니다. 콤보 박스는 IsTabStop = true 일 때 초점을 맞출 수 있습니다 (여기서 한 것처럼). 그 문제는 IsTabStop을 사용하여 컨트롤을 탐색 할 수 있는지 여부를 설정하는 편집 가능한 콤보 상자의 컨트롤 템플릿과 관련이 있다고 의심됩니다. –

+0

당신은 사실, 방금 연주했습니다 ... – Harry

관련 문제