【发布时间】:2013-12-11 13:46:17
【问题描述】:
我的 GUI 和线程有问题。 GUI 包含 DataGrid。每次程序执行一些查询并获取我想要填充到 DataGrid 中的项目列表。
到目前为止一切顺利:
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.
}
当用户单击数据网格中的一个对象时,线条颜色发生了变化(它工作正常),但是当程序重新加载表格时,绘制的线条消失了(因为我清除并添加了新对象)。
为了处理它,我创建了函数 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() 结束之前开始运行以调用所有对象并将新对象添加到数据网格中。
但如果是这样 - 为什么?我该如何解决?
最后,我希望在每次重新加载后立即重新绘制线..
感谢您的建议!
【问题讨论】:
标签: c# wpf multithreading datagrid