5

私はWindowそれにいくつかのコントロールを持っています。それらの1つはですDataGrid。デフォルト以外のフォーカス トラバーサルを実装したいと考えています。すなわち:

  • DataGrid各行ではなく、全体として 1 つのストップです。
  • にフォーカスがある場合DataGrid、ユーザーは上下のキーを使用して行間を移動できます。
  • 左右のキーを使用して列を移動することはできません。
  • 最初の列 (およびナビゲーションに関連する唯一の列) のタイプはDataGridHyperlinkColumnです。ユーザーが Space キーまたは Enter キーを押すと、ハイパーリンクが実行されます。

現時点では、次のコードがあります。

<DataGrid x:Name="DocumentTemplatesGrid"
          Grid.Row="2"
          ItemsSource="{Binding Source={StaticResource DocumentTemplatesView}}"
          IsReadOnly="True"
          AutoGenerateColumns="False"
          SelectionUnit="FullRow"
          SelectionMode="Single"
          TabIndex="1"
          IsTabStop="True">
  <DataGrid.CellStyle>
    <Style TargetType="DataGridCell">
      <Setter Property="IsTabStop" Value="False"/>
    </Style>
  </DataGrid.CellStyle>
  <DataGrid.RowStyle>
    <Style TargetType="DataGridRow">
      <Setter Property="IsTabStop" Value="False"/>
    </Style>
  </DataGrid.RowStyle>
  <DataGrid.Columns>
    <DataGridHyperlinkColumn Header="Name"
                             Width="2*"
                             Binding="{Binding Name}"/>
    <DataGridTextColumn Header="Description"
                        Width="5*"
                        Binding="{Binding Description}"/>
    <DataGridTextColumn Header="Type"
                        Width="*"
                        Binding="{Binding Type}"/>
  </DataGrid.Columns>
</DataGrid>

残念ながら、それは私の期待に達していません。これを達成する方法を説明していただけますか?

4

1 に答える 1

3

だから、あなたへの私の提案はこれです:

 <DataGrid x:Name="DocumentTemplatesGrid"
              Grid.Row="2"
              ItemsSource="{Binding Items}"
              IsReadOnly="True"
              AutoGenerateColumns="False"
              SelectionMode="Single"
              SelectionUnit="FullRow"
              TabIndex="1"
              IsTabStop="True"
              PreviewKeyDown="DocumentTemplatesGrid_PreviewKeyDown">
        <DataGrid.CellStyle>
            <Style TargetType="DataGridCell">
                <Setter Property="IsTabStop" Value="False"/>
                <Setter Property="BorderThickness" Value="0"/>
                <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
            </Style>
        </DataGrid.CellStyle>
        <DataGrid.RowStyle>
            <Style TargetType="DataGridRow">
                <Setter Property="IsTabStop" Value="False"/>
            </Style>
        </DataGrid.RowStyle>

DataGrid に PreviewKeyDown イベントを追加し、各セルからセル選択を削除しました。その結果、選択が行のみのように見えます。

コード ビハインドでは、これが Space / Enter でリンクを開くものです。

private void DocumentTemplatesGrid_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
    {
        if (e.Key == System.Windows.Input.Key.Space || e.Key == System.Windows.Input.Key.Enter)
        {
            if (e.Source is DataGrid)
            {
                string navigationUri = ((e.Source as DataGrid).SelectedItem as Class).Name;
                Process.Start(navigationUri);
            }
            e.Handled = true;
        }
    }

これがあなたが探しているものであるか、少なくともいくつかの助けになることを願っています.

于 2016-11-07T13:42:59.063 に答える