我有我的GUI和线程的问题。 GUI包含DataGrid。每X次该程序做一些查询并获取我想要填充到DataGrid中的项目列表。Wpf应用程序和线程
到目前为止好:
private void loadTaskList() //Call every X time
{
List<myObject> myList = myquery();
this.Dispatcher.Invoke((Action)(() =>
{
TaskListTable.Items.Clear(); //Clear the DataGrid
foreach (myObject O in myList) //Add the items from the new query.
{
TaskListTable.Items.Add(O);
}
}));
FindSelectionObject(); // <-- see next explanation.
}
当DataGrid中的对象之一的用户点击,线的颜色变化(正常工作),但是当程序重新加载表,该画线消失(我打算清除并添加新的对象)。
来对付它,我创建了功能FindSelectionObject():
private void FindSelectionObject()
{
this.Dispatcher.Invoke((Action)(() =>
{
this.SelectedIndex = TaskListTable.Items.IndexOf((myObject)lastSelectionObject); //find index of the new object that equels to the last selection object.
var row = TaskListTable.ItemContainerGenerator.ContainerFromIndex(SelectedIndex) as DataGridRow; //get the row with the index
row.Background = Brushes.LightGoldenrodYellow; //repaint
}));
}
问题:一切正常,但有时当程序重新加载,每秒线闪烁,然后强调了回来,有时它根本不画(直到下一次重新加载)。
我不明白为什么会这样。我想可能FindSelectionObject()
在loadTaskList()
结束之前开始运行,调用all并将新对象添加到数据网格中。 但如果是这样 - 为什么?我该如何解决它?
在底线,我想,经过每一个重载线重新漆立即..
感谢您的咨询!
谢谢@ odyss-jii。你的答案的第二部分是我以为发生了什么,我试过你的代码,但结果是它重新加载后从未突出行。我明白,我的解决方案是真的凌乱,但我没有其他的想法,任何提示? – Yaron
嗯,你可能想尝试'LoadingRow'事件。我已经用关于这方面的一些信息更新了答案。我希望这能更好地工作。 –
It Works!我注册到事件'LoadingRow'并运行'FindSelectionObject'。现在我甚至可以从'loadTaskList'中删除'FindSelectionObject'。谢谢@ odyss-jii! – Yaron