如何将具有多个项目的 IList 绑定到 Datagrid 而不会冻结

发布于 2024-11-18 09:18:16 字数 2990 浏览 4 评论 0原文

您好,我将 txt 文件解析为 List,然后我想将此集合绑定到 WPF 数据网格控件。

Txt 文件包含 18 000 - 19 000 行。这是该 txt 文件的示例。

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

我使用此函数将文本文件解析为 IList。

    public class Call
    {
        public string Number { get; set; }
        public string CallType { get; set; }
        public string Dt { get; set; }
        public string CallingNumber { get; set; }
        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;
    }

解析就OK了。

问题是,如果我尝试将方法解析的输出绑定到数据网格控件。 WPF 总是冻结。

我使用 WPF、.NET 4 和 Caliburn Micro 作为 MVVM 框架。

这是问题代码:

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

        }

如何将属性调用从 ViewModel 绑定到 DataGrid 控件而不冻结?

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; set; }
        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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丿*梦醉红颜 2024-11-25 09:18:16

一个可能的答案是不加载 18000 个项目。无论如何,没有人会先查看所有这些项目,然后再将其过滤到更易于管理的数字。

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.

肩上的翅膀 2024-11-25 09:18:16

我认为 VirtualizingStackPanel.IsVirtualizing 属性可以解决此问题:

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

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

<DataGrid Name="Calls" VirtualizingStackPanel.IsVirtualizing="True" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文