C#WPF同步选定的行在移动到顶部时不会突出显示



我正在使用Syncfusion WPF Datagrid来显示一些数据。我已经实现了将选定的行向上、向下、顶部和底部移动的功能。当移动该行时,仍应选择该行,这适用于向上、向下和到底,但由于某些原因,不适用于将该行移动到数据网格的顶部。我已经提供了下面的代码,我应该指出,保持所选行高亮显示的代码行对于向上、向下和到底是相同的。

public void PriorityMoveTop()
{
Job? SelectedRow = (Job)this.jobListView.SelectedItem;
if (SelectedRow == null)
{
}
else
{
Job? myJobAfterSelectedRow = (from x in Memory.Jobs where x.Priority == (SelectedRow.Priority - 1) select x).FirstOrDefault();
if (jobListView.SelectedIndex == 0)
{
}
else
{
foreach (Operation operation in SelectedRow.Operations)
{
operation.Priority = SelectedRow.Priority;
}
OrderPriorityColumnDown();
SelectedRow.Priority = 1;
}
}
SetDataGridJobList();
if (SelectedRow == null)
{
}
else
{
//Code to highlight row
jobListView.SelectedIndex = SelectedRow.Priority - 1;
}
}

这是我将行移动到Datagrid顶部的函数方法。有人能告诉我这行代码哪里出了问题吗?

jobListView.SelectedIndex = SelectedRow.Priority - 1;

我们怀疑当一行移动到SelectedRow中Priority值的顶部时,该值为零。在这种情况下,SelectedIndex被设置为-1。当SfDataGrid中的SelectedIndex值为-1时,所选行不会高亮显示。这是SfDataGrid的行为。因此,当一行移动到SelectedRow.Priority.的顶部时,你能检查SelectedIndex值吗

在Syncfusion团队的帮助下,我成功地让它工作起来。

我在Datagrid用户控制类中创建了一个方法:

public void MoveToTopRow()
{
var getSelectedIndex = 0;
jobListView.SelectedIndex = getSelectedIndex;
}

然后,我在我的MainWindow类中点击按钮时调用了这个方法:

private void btnArrowTop_Click(object sender, RoutedEventArgs e)
{ 
jobListView.PriorityMoveTop();
jobListView.MoveToTopRow();
jobListView.SetDataGridJobList();
jobListView.MoveToTopRow();
}

重置ItemSource后维护选择修复了它。

最新更新