【发布时间】:2012-01-13 11:36:46
【问题描述】:
当我进入 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>
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; }
}
}
点击文本框,然后点击 tab --- 单元格 1 未被选中
再次点击标签 --- 单元格 2 被选中
非常感谢任何帮助,谢谢。
更新:
当 SelectionUnit=FullRow 时,我在下面显示的几行中取得了一些成功,如果 SelectedIndex 在创建时设置为 0,那么现在第一行 被选择为蓝色。它仍然需要一些工作来处理 shift-tab 等。但是仍然存在一个问题,因为当我将 SelectionMode 更改为扩展并按 shift-downarrow 时,第二行被选中但第一行未被选中(它们都应该被选中) .如果我再次选择第 2+3 行,这是正确的,之后它会继续正常工作。
protected override void OnIsKeyboardFocusWithinChanged(DependencyPropertyChangedEventArgs e)
{
base.OnIsKeyboardFocusWithinChanged(e);
int oldIdx = this.SelectedIndex;
this.SelectedIndex = -1;
this.SelectedIndex = oldIdx;
}
进一步更新:
通过设置私有 _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);
}
}
【问题讨论】:
-
我认为你需要使用 hasfocus 但不是肯定的,所以我不发布作为答案。
-
奇怪的是,我在调试期间尝试窥探
DataGrid的属性时得到了StackOverflowException。