gpt4 book ai didi

c# - "Must create DependencySource on same Thread as the DependencyObject"创建GridView时

转载 作者:行者123 更新时间:2023-11-30 15:02:33 24 4
gpt4 key购买 nike

我的线程有问题。当我想将 GridView 设置为 ListView 作为另一个线程中的 View 时。它会显示一条消息:

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));

我在做什么?

最佳答案

正如错误所述,依赖属性及其对应的绑定(bind)必须在同一线程上创建。它不能设置在不同的线程上。也将网格的创建放在 UI 调度程序上。由于您的 ListView View DP 是在 UI 线程上创建的,因此它的源属性,即 GridView 也应该在 UI 线程上。

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
}));

关于c# - "Must create DependencySource on same Thread as the DependencyObject"创建GridView时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12554612/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com