2017-03-23 53 views
0

我正在一個WPF項目,需要一個2D網格,需要擴大和縮小窗口內。爲了做到這一點,我用UniformGrids替換了DataGrid中的ItemsPanel。當我這樣做時,控件會正確展開和收縮。WPF DataGrid的DataGridRow覆蓋ItemsPanel中斷製表訂單

不幸的是,出於某種原因,這打破了DataGrid中的Tab鍵順序。如果按Tab鍵,它將下降一行,而不是直接跳到下一列。當標籤到達行的末尾時,它將轉到下一列的頂部並繼續向下。左,右,上,下箭頭鍵均按預期工作,受影響的唯一功能是Tab鍵順序。如果我刪除了DataGridRow樣式,Tab鍵順序會自行修正,但行不會隨着窗口展開。

這個問題似乎並沒有被隔離到UniformGrid,因爲StackPanel也會產生相同的選項卡症狀。

有沒有人知道我應該尋找什麼來解決這個標籤問題或可能的另一種方式來使網格擴展和合同按要求?

例XAML:

<Window x:Class="SODatagridSample.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 
<Window.Resources> 
    <Style TargetType="{x:Type DataGridRow}"> 
     <Setter Property="ItemsPanel"> 
      <Setter.Value> 
       <ItemsPanelTemplate> 
        <UniformGrid IsItemsHost="True" Rows="1" /> 
       </ItemsPanelTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</Window.Resources> 
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"> 
    <Grid> 
     <DataGrid x:Name="datagrid" ItemsSource="{Binding Values}" SelectionMode="Extended" SelectionUnit="Cell" ColumnWidth="*" HeadersVisibility="None" 
        CanUserAddRows="False" CanUserDeleteRows="False"> 
      <DataGrid.ItemsPanel> 
       <ItemsPanelTemplate> 
        <UniformGrid Columns="1"></UniformGrid> 
       </ItemsPanelTemplate> 
      </DataGrid.ItemsPanel> 
     </DataGrid> 
    </Grid> 
</ScrollViewer> 

示例代碼後面:

using System.ComponentModel; 
using System.Data; 
using System.Windows; 

namespace SODatagridSample 
{ 
    public partial class MainWindow : Window, INotifyPropertyChanged 
    { 
     public DataTable Values 
     { 
      get { return _Values; } 
      set 
      { 
       _Values = value; 
       OnPropertyChanged(nameof(Values)); 
      } 
     } 
     private DataTable _Values; 

     public MainWindow() 
     { 
      Values = new DataTable(); 

      for (int i = 0; i < 15; i++) 
       Values.Columns.Add(i.ToString(), typeof(double)); 

      for (int i = 0; i < 10; i++) 
       Values.Rows.Add(Values.NewRow()); 

      for (int x = 0; x < 10; x++) 
       for (int y = 0; y < 15; y++) 
        Values.Rows[x][y] = x * 15 + y; 

      DataContext = this; 
      InitializeComponent(); 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 

     public void OnPropertyChanged(string name) 
     { 
      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); 
     } 
    } 
} 

回答

0

的修復是超級簡單。反映了DataGridCellPanel後,我發現了以下靜態構造函數:

static DataGridCellsPanel() 
     { 
      KeyboardNavigation.TabNavigationProperty.OverrideMetadata(typeof(DataGridCellsPanel), new FrameworkPropertyMetadata((object)KeyboardNavigationMode.Local)); 
     } 

所有這一切都需要的是可以擴展UniformGrid補充一點,靜態構造函數或只是添加附加屬性KeyboardNavigation.TabNavigation =「本地」的DataGridRow風格中的UniformGrid:

 <Style TargetType="{x:Type DataGridRow}"> 
      <Setter Property="ItemsPanel"> 
       <Setter.Value> 
        <ItemsPanelTemplate> 
         <UniformGrid KeyboardNavigation.TabNavigation="Local" IsItemsHost="True" Rows="1"/> 
        </ItemsPanelTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style>