WPF:将大型集合绑定到GridControl

时间:2015-01-12 10:19:00

标签: c# wpf multithreading data-binding

我刚刚开始我的第一个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);
        }
    }

因为我希望我的应用程序能够对用户保持响应,所以我一直在寻找另一种方法来一次性绑定这些数据。所以我想到了这个想法。当接口发生锁定时,是否可以暂停绑定操作,以便接口可以自行更新?我很喜欢编程,所以请原谅我的无知:)

谢谢~~

1 个答案:

答案 0 :(得分:1)

DevExpress GridControl支持数据虚拟化,只有构建在屏幕上可见的控件/项目并添加到可视树中。此外,当您向下滚动列表时,这些可见控件通常会被回收,从而节省了重建它们的成本。

如果您不熟悉虚拟化,请考虑一个简单的列表视图示例:您可以绑定一个包含10,000个项目的数据源,但在任何时候用户只能看到20个项目。虚拟化机制确保仅创建可见的列表项。如果没有此功能,列表框将必须创建10,000个WPF列表项(反过来可能包含多个控件或UI元素),并将它们保存在内存中并将它们添加到可视树中,即使它们不可见。 WPF控件只能添加到UI线程上的可视树中,这将导致代码挂起。

看起来DevExpress GridControl支持开箱即用的虚拟化,但可以通过使用自己的DevExpress数据源类来增强它。 Check out this documentation ...您可能需要使用LinqServerModeDataSourceLinqInstantFeedbackDataSource课程来为您提供所需的表现。