“必须在与DependencyObject相同的线程上创建DependencySource".创建GridView时 [英] "Must create DependencySource on same Thread as the DependencyObject" When Create GridView
问题描述
我的线程有问题,当我想将GridView设置为ListView的另一个线程中的View时,它会显示一条消息:
I have a problem with thread.When I want set a GridView into a ListView as View in another thread.It display a message which said:
必须在与DependencyObject相同的线程上创建DependencySource.
Must create DependencySource on same Thread as the DependencyObject.
// Create grid view
GridView grid = new GridView();
// Add column
// Name
grid.Columns.Add((GridViewColumn)myresourcedictionary["gridDirFileName"]);
// Type
grid.Columns.Add((GridViewColumn)myresourcedictionary["gridDirFileType"]);
// Data Modified
grid.Columns.Add((GridViewColumn)myresourcedictionary["gridDirFileDataModified"]);
// Size
grid.Columns.Add((GridViewColumn)myresourcedictionary["gridDirFileSize"]);
// Edit view
Application.Current.Dispatcher.Invoke(new Action(() => ListViewOp.View = grid));
我在做什么?
推荐答案
错误提示Dependency Property and its corresponding binding have to be created on same thread
.不能在不同的线程上设置它.将网格的创建也放在UI调度程序上.由于您的ListView View
DP是在UI线程上创建的,因此它的源属性(即GridView
)也应该在UI线程上.
As the error says Dependency Property and its corresponding binding have to be created on same thread
. It can't be set on different threads. Put the creation of grid on UI dispatcher too. Since your ListView View
DP is created on UI thread, hence its source property i.e. GridView
should also be on UI thread.
Application.Current.Dispatcher.Invoke((Action)(delegate
{
GridView grid = new GridView();
grid.Columns.Add((GridViewColumn)myresourcedictionary["gridDirFileName"]);
grid.Columns.Add((GridViewColumn)myresourcedictionary["gridDirFileType"]);
grid.Columns.Add((GridViewColumn)myresourcedictionary["gridDirFileDataModified"]);
grid.Columns.Add((GridViewColumn)myresourcedictionary["gridDirFileSize"]);
ListViewOp.View = grid
}));
这篇关于“必须在与DependencyObject相同的线程上创建DependencySource".创建GridView时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!