Control.AddRange(…) is slow

后端 未结 1 500
粉色の甜心
粉色の甜心 2020-11-30 13:06

Project: I have a parent panel which holds a ComboBox and FlowLayoutPanel. The FlowLayoutPanel holds a variable number of child panels (a custom control th

1条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-30 14:01

    Posting this answer because the OP requested it:

    This is how you'd do something like that in WPF:

    
        
            

    Code Behind (only boilerplate to support the example)

    public partial class ListBoxSample : UserControl
    {
        public ListBoxSample()
        {
            InitializeComponent();
        }
    
        public void LoadData()
        {
            Task.Factory.StartNew(() =>
            {
                var list = new List();
    
                for (int i = 0; i < 100000; i++)
                {
                    var item = new DataItem()
                    {
                        From = "1",
                        To = "2",
                        ChildItems =
                        {
                            new ChildItem()
                            {
                                DependeeFrom = i.ToString(),
                                DependeeTo = (i + 10).ToString(),
                                XXXX = "XXXX"
                            },
                            new ChildItem()
                            {
                                DependeeFrom = i.ToString(),
                                DependeeTo = (i + 10).ToString(),
                                XXXX = "XXXX"
                            },
                            new ChildItem()
                            {
                                DependeeFrom = i.ToString(),
                                DependeeTo = (i + 10).ToString(),
                                XXXX = "XXXX"
                            }
                        }
                    };
    
                    list.Add(item);
                }
                return list;
    
            }).ContinueWith(t =>
            {
                Dispatcher.Invoke((Action) (() => DataContext = t.Result));
            });
        }
    
        private void Load_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            LoadData();
        }
    }
    

    Data Items:

    public class DataItem
    {
        public List ChildItems { get; set; }
    
        public List FromOptions { get; set; }
    
        public List ToOptions { get; set; }
    
        public string From { get; set; }
    
        public string To { get; set; }
    
        public DataItem()
        {
            ChildItems = new List();
    
            FromOptions = Enumerable.Range(0,10).Select(x => x.ToString()).ToList();
            ToOptions = Enumerable.Range(0, 10).Select(x => x.ToString()).ToList();
        }
    }
    
    public class ChildItem
    {
        public string XXXX { get; set; }
    
        public string DependeeFrom { get; set; }
    
        public string DependeeTo { get; set; }
    }
    

    Then you put that in an existing winforms UI using an ElementHost:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
    
            var elementHost = new ElementHost
            {
                Dock = DockStyle.Fill,
                Child = new ListBoxSample()
            };
    
            Controls.Add(elementHost);
    
        }
    }
    

    Result:

    enter image description here

    • Notice that I added 100,000 records. Still, response time (both when scrolling and interacting with the UI) is immediate due to WPF's built in UI Virtualization.
    • Also notice that I'm using DataBinding which removes the need to manipulate UI elements in procedural code. This is important because the WPF Visual Tree is a complex structure, and DataBinding is the preferred approach in WPF always.
    • Also notice by resizing the form that the UI is completely resolution independent. You can customize it further by making the ComboBoxes fixed and having the DataGrid stretch to the remaining space. See WPF Layouts.
    • WPF Rocks. - see how much you can achieve with so little code, and without spending lots of $$$ in third party controls. You should really forget winforms forever.
    • You will need to target .Net 3.0 at a minimum, but 4.0/4.5 is highly recommended because WPF had several issues in earlier versions, which were fixed in 4.0.
    • Make sure you reference PresentationCore.dll, PresentationFramework.dll, WindowsBase.dll, System.Xaml.dll and WindowsFormsIntegration.dll, all of which belong to the .Net Framework itself (no 3rd parties)

    0 讨论(0)
提交回复
热议问题