Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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
        });
 }
Posted
Updated 6-May-16 1:26am
v3
Comments
CHill60 6-May-16 4:49am    
Put the items into a datatable or list and bind the control to that
Hrishikesh Ranjit Shivacharan 6-May-16 6:02am    
Datatable is very easy way to bind the data to Itemssource.
VR Karthikeyan 6-May-16 7:06am    
Just declare a list of type FileListItem, or an observablecollection of type FileListItem and add every item in this collection or list in your foreach loop. after finishing, just bind it to ItemsSource of ListView.

1 solution

Hi, just change your ListView code in xaml like below,
XML
<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,
C#
ObservableCollection<FileListItem> FileListItemsCollection = new ObservableCollection<FileListItem>();

foreach (FilePaths filePath in directoryPath.GetFilePaths())
{
    GetFileListViewItem(filePath);
}
fileListView.ItemsSource = FileListItemsCollection;

And,
C#
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.
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900