c# - WPF DataGrid 移动到最后选定的行而不突出显示最近单击的行

标签 c# wpf datagrid

我有 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/

相关文章:

C# 枚举为 int

c# - 日期时间 DayOfWeek 比较

c# - 大批量 Entity Framework 更新比我自己批处理慢得多

c# - 为什么后台垃圾收集有时会暂停我的应用程序,我该如何防止它发生?

wpf - 绑定(bind)到集合中的单个元素

gwt - 如何使 GWT Datagrid 的第一列固定并水平和垂直滚动

c# - Poloniex C# API - 尝试提取 ETH 时出错

WPF 数据网格绑定(bind)

WPF Datagrid 在禁用控件时取消选择行

.net - 将行颜色绑定(bind)到 MVVM 中的属性(取决于行中的数据)