我有 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# 代码中我在 Debug模式下检查它)但在 WPF DataGrid 中仍然突出显示新单击的行。
如果我不够清楚,请告诉我。
谢谢
最佳答案
您可以使用 DataGrid.SelectedCells 清除选定的单元格属性(property)。
就这样
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 的实例。
关于c# - WPF DataGrid 移动到最后选定的行而不突出显示最近单击的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30711536/