【发布时间】:2015-01-12 10:18:59
【问题描述】:
我刚刚开始我的第一个 WPF 项目,今天早上我遇到了一个问题。 有这么多位置 (50.000) 我想绑定到 GridControl。
public void BindData()
{
//disabling the control seemed to shorten the UI lock.
gcLocations.IsEnabled = false;
Task.Factory.StartNew(() =>
{
gcLocations.SetPropertyThreadSafe("ItemsSource", OceanData.OceanPorts.Values);
});
gcLocations.IsEnabled = true;
}
public static void SetPropertyThreadSafe(this Control control, string propertyName, object value)
{
Type type = control.GetType();
var prop = type.GetProperty(propertyName);
if(prop == null)
{
throw new Exception(string.Format("No property has been found in '{0}' with the name '{1}'", control, propertyName));
}
object[] param = new object[] { propertyName, prop.PropertyType, value };
if(prop.PropertyType != typeof(object) && prop.PropertyType != value.GetType())
{
throw new Exception(string.Format("Property types doesn't match - property '{0}' (type:{1}) and value '{2}'(type:)", param));
}
if(control.Dispatcher.CheckAccess())
{
prop.SetValue(control, value);
}
else
{
control.Dispatcher.BeginInvoke(new Action(() =>
{
prop.SetValue(control, value);
}), DispatcherPriority.ContextIdle, null);
}
}
因为我希望我的应用程序保持对用户的响应,所以我一直在寻找一种替代方法来一次性绑定这些数据。所以我想到了这个想法..是否可以在界面发生锁定时暂停绑定操作,以便界面可以自行更新?我对编程很陌生,所以请原谅我的无知:)
谢谢~~
【问题讨论】:
-
据我所知,您的代码正在向 GridControl 添加 50'000 多个控件……这需要一些时间,而且会在 UI 线程上发生!是否需要同时显示所有 50'000 个控件?还是滚动查看它们?这个问题急需对您的数据进行某种虚拟化......
-
尝试使用
ListView进行这种显示,并使用GridView为您的控件定义自定义视图。当您使用ListView时,您可以使用VirtualisingStackPanel的好处,它提供了虚拟化,因此得名:-)。如果您需要更多信息,请告诉我。 -
感谢您的回复! @olitee 一点也不。但在某些情况下,用户需要搜索位置并编辑属性。我使用 Devexpress 网格控件,它具有非常好的过滤数据。不幸的是,列表视图没有这个功能。我必须为此创建一个自定义控件,所以我宁愿继续使用 gridcontrol。
-
DevExpress 的 GridControl 应默认为开箱即用地虚拟化您的数据。您是否已经尝试过简单地将值集合数据绑定到您的 ItemsSource?什么是底层集合?它已经在内存中了吗?
-
@olitee,我目前正在将值绑定在内存中的 Directory
中。 “默认为开箱即用虚拟化数据”是什么意思?
标签: c# wpf multithreading data-binding