WPF TAB在DataGrid之间的循环

时间:2019-07-03 13:30:27

标签: c# wpf xaml datagrid

在WPF DataGrid的默认TAB导航中,循环单元/行。我的DataGrid都是只读的,它们的选择单位是行,因此这种默认行为没有任何意义。我该如何在DataGrids之间循环?
以下示例有些可以工作,但是在辅助DataGrid能够将焦点集中在 TAB 上并接受箭头键以循环选定的行之前,需要鼠标交互:

<Grid KeyboardNavigation.TabNavigation="Cycle">
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Grid.Resources>
        <Style TargetType="{x:Type DataGridCell}">
            <Setter Property="IsTabStop" Value="False"/>
        </Style>
    </Grid.Resources>
    <DataGrid Name="MasterDG" ItemsSource="{Binding Items}" IsReadOnly="True" Margin="10"
              SelectionUnit="FullRow" KeyboardNavigation.TabNavigation="Once" 
              IsTabStop="True" FocusManager.IsFocusScope="True"
              TabIndex="1">            
    </DataGrid>
    <DataGrid Grid.Column="1" ItemsSource="{Binding SelectedItem.Details, ElementName=MasterDG}" IsReadOnly="True" Margin="10"
              SelectionUnit="FullRow" KeyboardNavigation.TabNavigation="Once"
              IsTabStop="True" FocusManager.IsFocusScope="True"
              TabIndex="2"/>
</Grid>

此外,它非常复杂:

  1. 在父级上定义TabNavigation
  2. 在每个单元格上设置IsTabStop = false
  3. 在每个DataGrid上设置IsTabStop = true
  4. 设置FocusManager.IsFocusScope s
  5. 在每个DataGrid上覆盖默认的TabNavigation

如何修复示例,以使其在两个DataGrid之间正确循环?


示例中使用的代码隐藏:

public partial class MainWindow : Window
{
    public class Master
    {
        public int Value { get; set; }
        public IEnumerable<Detail> Details { get; set; }
    }
    public class Detail
    {
        public int Value { get; set; }
    }
    public List<Master> Items => new List<Master>()
    {
        new Master() {
            Value = 1,
            Details = new List<Detail>() { new Detail() { Value = 11 }, new Detail() { Value = 12 }, new Detail() { Value = 13 }, }
        },
        new Master() {
            Value = 2,
            Details = new List<Detail>() { new Detail() { Value = 24 }, new Detail() { Value = 25 }, new Detail() { Value = 26 }, }
        },
        new Master() {
            Value = 3,
            Details = new List<Detail>() { new Detail() { Value = 37 }, new Detail() { Value = 18 }, new Detail() { Value = 19 }, }
        },
    };

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
    }
}

0 个答案:

没有答案