1

私は 2UserControlsつ持ってTreeViewButtonますDataGrid

私が達成しようとしているのは、2 番目の UserControl で KeyBoard フォーカスを DataGrid に与える必要がある場合TabですTreeViewItem

さまざまな投稿を調べましたが、運がありません。以下の私のXAMLを見つけて、

<Grid>
  <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />            
    </Grid.RowDefinitions>
   <UC:ExplorerView DataContext="{Binding ExplorerViewModel}" Grid.Row="0"/>
   <UCs:TableOfContentView DataContext="{Binding TableOfContentViewModel}" x:Name="TOCView" Grid.Row="1"/>
</Grid>

質問の簡略化された XAML。

UserControlイベントを追加して、フォーカスを秒に設定しようとしましたPreviewKeyDown

 private void ExplorerView_KeyDown(object sender, KeyEventArgs e)
    {
        if(e.Key == Key.Tab)
        {                
            Keyboard.Focus(TOCView);
        }
    }

しかし、これにより、上記のように DataGrid ではなく USerControl に焦点が当てられます。

にしようとAttachPropertyましたDataGrid。期待どおりに機能しましたが、に焦点を当てていませんでしたfirst rowこのスレッドはそのための入力を与えられました。


解決しました:)

上記のスレッドで言及されているものを作成しAttachedProperty、コールバック メソッドを変更して、DataGrids以下のように最初の行にフォーカスを設定しました。

  private static object OnCoerceValue(DependencyObject d, object baseValue)
    {  
        if (((DataGrid)d).HasItems)
        {
            DataGridRow row = ((DataGrid)d).ItemContainerGenerator.ContainerFromIndex(0) as DataGridRow;
            if(row !=null)
            { 
                if ((bool)baseValue)
                {
                    FocusManager.SetIsFocusScope(row, true);
                    FocusManager.SetFocusedElement(row, row);
                }
                else if (((UIElement)d).IsFocused)
                    Keyboard.ClearFocus();
            }
        }
        return ((bool)baseValue);            
    }

より良い解決策を自由に追加してください。前もって感謝します。

4

1 に答える 1

2

Created the AttachedProperty mentioned in the thread above and modified the callback method to set the focus to the DataGrids First Row like below,

  private static object OnCoerceValue(DependencyObject d, object baseValue)
    {  
        if (((DataGrid)d).HasItems)
        {
            DataGridRow row = ((DataGrid)d).ItemContainerGenerator.ContainerFromIndex(0) as DataGridRow;
            if(row !=null)
            { 
                if ((bool)baseValue)
                {
                    FocusManager.SetIsFocusScope(row, true);
                    FocusManager.SetFocusedElement(row, row);
                }
                else if (((UIElement)d).IsFocused)
                    Keyboard.ClearFocus();
            }
        }
        return ((bool)baseValue);            
    }
于 2015-11-24T20:28:51.650 に答える