我有DataGrid并添加了如下事件,执行一些验证计算,如果验证成功则无关。但是如果验证失败,那么突出显示的行应该仍然是最后选择的行,而不是新点击的行: -
public void dataGridTable_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (dataGrid.SelectedIndex == lastSelectedIndex)
{
return;
}
/*
Validate method will return true if row validation is fine else false.
*/
if(!validate())
{
// Revert to last selected index.
dataGrid.SelectedIndex = lastSelectedIndex;
}
}
dataGrid.SelectedIndex = lastSelectedIndex;
此代码将选定的索引作为最后选择的索引(在c#代码中我在调试模式下检查它)但在WPF DataGrid中仍然突出显示新单击的行。
如果我不够清楚,请告诉我。
谢谢
答案 0 :(得分:1)
您可以使用DataGrid.SelectedCells属性清除所选单元格。
只做
this.SelectedCells.Clear();
清除当前选择。
或者,如果要在SelectedIndex的当前行中选择单元格,则可以执行
this.SelectAllCells();
DataGridCellInfo[] cells = this.SelectedCells.Where(x => this.Items.IndexOf(x.Item) == this.SelectedIndex).ToArray();
this.SelectedCells.Clear();
foreach (DataGridCellInfo cell in cells)
{
this.SelectedCells.Add(cell);
}
其中this
是DataGrid的实例。