嗨,我将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;
}解析是可以的。
问题是,如果我尝试将mehod分析的输出绑定到datagrid控件中。WPF总是冻僵。
我使用WPF,.NET 4和杯标微作为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控件而不冻结?
发布于 2012-11-07 18:53:44
一个可能的答案是简单地不加载18000项。不管怎么说,在过滤到一个更容易管理的数字之前,没有人会仔细检查所有这些项目。
发布于 2011-11-04 20:38:48
我认为VirtualizingStackPanel.IsVirtualizing属性可以解决这个问题:
<DataGrid Name="Calls" VirtualizingStackPanel.IsVirtualizing="True" />https://stackoverflow.com/questions/6562033
复制相似问题