当你第一次选中一个单元格时,如何让WPF Datagrid选择一个单元格

Fra*_*lis 10 wpf datagrid

当我选中WPF Datagrid时,它会聚焦第一个单元格(带有一个矩形)但不选择它(蓝色).如果我再次按Tab键,它会聚焦选择它.

我认为DataGridCell实际上有IsSelected = true,但它没有被涂成蓝色.我曾尝试使用数据网格和视觉状态进行黑客攻击,但是当你第一次选中时,我无法正确地重新绘制网格.

有没有人见过这个,你有解决方案吗?

代码重现:

MainWindow.xaml

<Window x:Class="WpfApplication1.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">
    <StackPanel>
        <TextBox Width="100"/>
        <DataGrid SelectionMode="Single" SelectionUnit="Cell"
              ItemsSource="{Binding MyItems}" AutoGenerateColumns="True"/>
    </StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)

MainWindow.xaml.cs

using System.Collections.Generic;
using System.Windows;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            MyItems.Add(new Thingy() { Name = "Frank", Age = 34 });
            MyItems.Add(new Thingy() { Name = "Jim", Age = 43 });
            MyItems.Add(new Thingy() { Name = "Bob", Age = 56 });
            MyItems.Add(new Thingy() { Name = "Harry", Age = 23 });

            DataContext = this;
        }

        private List<Thingy> _myItems = new List<Thingy>();
        public List<Thingy> MyItems
        {
            get { return _myItems; }
        }
    }

    public class Thingy
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)

单击TextBox,然后单击选项卡---未选择单元格1

再次点击标签---选择单元格2

非常感谢任何帮助,谢谢.

更新:

当SelectionUnit = FullRow时,我沿着下面显示的线条取得了一些成功,如果在创建时将SelectedIndex设置为0,现在选择蓝色的第一行.仍然需要一些工作来处理shift-tab等.但是仍然存在一个问题,因为当我将SelectionMode更改为extended并按下shift-downarrow时,第二行被选中但第一行未被选中(它们都应被选中) .如果我再次选择行2 + 3,这是正确的,然后继续工作正常.

protected override void OnIsKeyboardFocusWithinChanged(DependencyPropertyChangedEventArgs e)
    {
        base.OnIsKeyboardFocusWithinChanged(e);

        int oldIdx = this.SelectedIndex;
        this.SelectedIndex = -1;
        this.SelectedIndex = oldIdx;
    }
Run Code Online (Sandbox Code Playgroud)

进一步更新:

通过设置private _selectionAnchor字段修复了该问题.(谢谢ILSpy)

 protected override void OnIsKeyboardFocusWithinChanged(DependencyPropertyChangedEventArgs e)
    {
        base.OnIsKeyboardFocusWithinChanged(e);

        this.SelectedIndex = -1;
        this.SelectedIndex = 0;

        SelectionAnchor = SelectedCells[0];
    }

    protected DataGridCellInfo? SelectionAnchor
    {
        get
        {
            return typeof(DataGrid).GetField("_selectionAnchor", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(this) as DataGridCellInfo?;
        }
        set
        {
            typeof(DataGrid).GetField("_selectionAnchor", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(this, value);
        }
    }
Run Code Online (Sandbox Code Playgroud)

小智 5

我知道我的回答为时已晚,但它会帮助其他人导航到此站点。

经过大量研究,我得到了如何在使用 Tab 键时选择元素的答案。这真的很简单,只需一行 XAML 代码就可以解决问题;

 <Style TargetType="{x:Type DataGridCell}">
     <Setter Property="IsTabStop" Value="False"/>
 </Style>
Run Code Online (Sandbox Code Playgroud)

通过设置IsTabStop为 false,您告诉 datagridcell 的可视化树进入其模板并找到任何可以聚焦的元素。如果它找到某个元素,则它会聚焦该元素。


Gur*_*ruC 4

你可以这样做。注册获得焦点事件,然后将原始源设置为所选项目。

<Window x:Class="WpfApplication1.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">
<StackPanel>
    <TextBox Width="100"/>
    <DataGrid SelectionMode="Single" SelectionUnit="Cell"
          ItemsSource="{Binding MyItems}" AutoGenerateColumns="True"  
            GotFocus="WPF_DataGrid_GotFocus" />
</StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)

然后在文件后面的代码中:

    private void WPF_DataGrid_GotFocus(object sender, RoutedEventArgs e)
    {
        (e.OriginalSource as DataGridCell).IsSelected = true;

    }
Run Code Online (Sandbox Code Playgroud)

我希望它有帮助!