开发者

How bind IList with many items to Datagrid without freezing

开发者 https://www.devze.com 2023-03-17 10:47 出处:网络
Hi I parse txt file to List and then I want bind this collection to WPF datagrid control. Txt file contain 18 000- 19 000 rows. Here is sample of this txt file.

Hi I parse txt file to List and then I want bind this collection to WPF datagrid control.

Txt file contain 18 000 - 19 000 rows. Here is sample of this txt file.

910839320   Hovory vo VPS   24.05.2011 08:52    0910839253  VPS     STÁLA   00:01:03            0,0488  
910839320   Hovory vo VPS   26.05.2011 10:01    0910839267  VPS     STÁLA   00:01:00            0,0465  
910839320   Hovory vo VPS   26.05.2011 10:04    0910839263  VPS     STÁLA   00:00:19            0,0147

I parse text file to IList with this function.

    public class Call
    {
        public string Number { get; set; }
        public string CallType { get; set; }
        public string Dt { get; set; }
        public string CallingNumber { get; s开发者_开发问答et; }
        public string VoiceNetwork { get; set; }
        public string Type { get; set; }
        public string TalkTime { get; set; }
        public string Price { get; set; }
    }

    private static IEnumerable<Call> Parse(string path)
    {
        var calls = new List<Call>();

        string[] lines = System.IO.File.ReadAllLines(path);

        for (int i = 0; i < lines.Length; i++)
        {
            string[] line = lines[i].Split('\t');

            var call = new Call
                           {
                               Number = line[0],
                               CallType = line[1],
                               Dt = line[2],
                               CallingNumber = line[3],
                               VoiceNetwork = line[4],
                               Type = line[6],
                               TalkTime = line[7],
                               Price = line[10]
                           };
            calls.Add(call);
        }

        return calls;
    }

Parsing is OK.

Problem is if I try bind output of mehod Parse to datagrid control. WPF always freeze.

I use WPF, .NET 4 and Caliburn Micro as MVVM framework.

Here is problem code:

XAML:

        <StackPanel Orientation="Vertical" Grid.Column="1">
            <DataGrid Name="Calls"/>
        </StackPanel>

ViewModel:

        public IList<Call> Calls
        {
            get { return _class; }
            set
            {
                _class = value;
                NotifyOfPropertyChange(()=>Calls);
            }
        }

        public void OpenTsv()
        {
            Task.Factory.StartNew(() =>
            {
                var dlg = new Microsoft.Win32.OpenFileDialog 
{ DefaultExt = ".tsv", Filter = "Tsv documents (.tsv)|*.tsv" };

                bool? result = dlg.ShowDialog();

                IList<Call> calls = new List<Call>();

                if (result == true)
                {
                    Path = dlg.FileName;
                    calls = TsvParser.Parse(Path);
                }

                Execute.OnUIThread((System.Action)(() =>
                { Calls = calls; }));
            });

        }

How to bind property Calls from ViewModel to DataGrid control without freezing?


A possible answer is to simply not load 18000 items. No person will look through all those items before filtering it down to a much more manageable number anyway.


I think the VirtualizingStackPanel.IsVirtualizing property would resolve this problem:

<DataGrid Name="Calls" VirtualizingStackPanel.IsVirtualizing="True" />
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号