列表视图中的添加项目在WPF中太慢 [英] Add item in listview is too slow in WPF
本文介绍了列表视图中的添加项目在WPF中太慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的问题是我需要在WPF中向Listview添加很多项目。在WinForms中,您只需使用BeginUpdate()方法,添加所有内容,最后使用EndUpdate()方法。
那么,我将如何停止绘图一个WPF Listview,直到每个项目都被添加,然后一次性绘制所有内容?
我尝试过:
foreach (FilePaths filePath in directoryPath.GetFilePaths())
{
GetFileListViewItem(filePath);
}
在此 GetFileListViewItem方法中,i 将项添加到listview。
private void GetFileListViewItem(FilePaths filePath)
{
string ext = GetExtension(filePath.GetPath());
string fileName = GetFileNameWithoutExtension(filePath.GetPath());
string type = ;
if (\\ text = = )
{
type = ext.ToUpper()。Substring( 1 )+ 档案;
}
其他
{
type = 未知;
}
fileListView.Items.Add( new FileListItem
{
Name = fileName,
Type = type
});
}
解决方案
您好,只需更改下面的xaml中的ListView代码,
< pre lang =xml> < ListView x:名称 = listView 高度 = 249 宽度 = 497 >
< ListView.View >
< GridView >
< GridViewColumn 标题 = 文件名 DisplayMemberBinding = < span class =code-keyword> {Binding Name} / >
< GridViewColumn 标题 = 类型 DisplayMemberBinding = {绑定Ty pe} / >
< / GridView >
< / ListView.View >
< / ListView >
并且像这样更改你的代码,
的ObservableCollection< FileListItem> FileListItemsCollection = new ObservableCollection< FileListItem>();
foreach (FilePaths filePath in directoryPath.GetFilePaths())
{
GetFileListViewItem(filePath);
}
fileListView.ItemsSource = FileListItemsCollection;
并且,
私人 void GetFileListViewItem(FilePaths filePath)
{
string ext = GetExtension( filePath.GetPath());
string fileName = GetFileNameWithoutExtension(filePath.GetPath());
string type = ;
if (\\ text = = )
{
type = ext.ToUpper()。Substring( 1 )+ 档案;
}
其他
{
type = 未知;
}
FileListItemsCollection.Add( new FileListItem
{
Name = fileName,
Type = type
});
}
全局声明 FileListItemsCollection 。
My problem is that I need to add a lot of items to a Listview in WPF. In WinForms you'd just use the BeginUpdate() method, add everything, and finally use the EndUpdate() method.
So, how would I stop the drawing in a WPF Listview until every item is added and then draw everything in one go?
What I have tried:
foreach (FilePaths filePath in directoryPath.GetFilePaths())
{
GetFileListViewItem(filePath);
}
In this GetFileListViewItem method, i add the item to listview.
private void GetFileListViewItem(FilePaths filePath)
{
string ext = GetExtension(filePath.GetPath());
string fileName = GetFileNameWithoutExtension(filePath.GetPath());
string type = "";
if (ext != "")
{
type = ext.ToUpper().Substring(1) + " File";
}
else
{
type = "Unknown";
}
fileListView.Items.Add(new FileListItem
{
Name = fileName,
Type = type
});
}
解决方案
Hi, just change your ListView code in xaml like below,
<ListView x:Name="listView" Height="249" Width="497"> <ListView.View> <GridView> <GridViewColumn Header="File Name" DisplayMemberBinding="{Binding Name}" /> <GridViewColumn Header="Type" DisplayMemberBinding="{Binding Type}" /> </GridView> </ListView.View> </ListView>
And change your code like this,
ObservableCollection<FileListItem> FileListItemsCollection = new ObservableCollection<FileListItem>(); foreach (FilePaths filePath in directoryPath.GetFilePaths()) { GetFileListViewItem(filePath); } fileListView.ItemsSource = FileListItemsCollection;
And,
private void GetFileListViewItem(FilePaths filePath) { string ext = GetExtension(filePath.GetPath()); string fileName = GetFileNameWithoutExtension(filePath.GetPath()); string type = ""; if (ext != "") { type = ext.ToUpper().Substring(1) + " File"; } else { type = "Unknown"; } FileListItemsCollection.Add(new FileListItem { Name = fileName, Type = type }); }
declare FileListItemsCollection globally.
这篇关于列表视图中的添加项目在WPF中太慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文