In C# WPF, is there a way to make it sort to the top of the listview whenever a new item is added?

JunYoungLee 161 Reputation points
2022-05-31T16:21:57.613+00:00

I'm making a WPF app that implements a folder explorer.
And I want to add a new folder item to the top of the folder list view when the new folder button is pressed.
So I put a code that adds a sortdirection to sort by date, folder, and filename.
However, although the new folder is added to the top, there is a problem that the sort order of existing items is changed.
So, I would like to know how to make the existing items are sorted on top while new items are added. I know it is possible by using customsort, but it is difficult to implement with my skills.
Can anyone help me?

Below is the function code to add a new folder.

private void LocalNewFolder(int set_flag)
        {
            if (set_flag > 2) return;
            if (uiList_textbox[set_flag - 1].Text == "") return;

            var str_newfolder = "";
            NewFolderInputBox Local_Mk_NewFolder = new NewFolderInputBox();
            if (Local_Mk_NewFolder.ShowDialog() == true)
            {
                str_newfolder = Local_Mk_NewFolder.Text_NewFolder.Text;

                if (str_newfolder == "")
                    return;
            }
            else
            {
                return;
            }
            string path_newfolder = uiList_textbox[set_flag - 1].Text + "\\" + str_newfolder;
            //string path_newfolder = this.line_set1_local.Text + "\\" + str_newfolder;

            if (!Directory.Exists(path_newfolder))
            {
                Directory.CreateDirectory(path_newfolder);
            }
            else
            {
                return;
            }

            var folderdate = Directory.GetLastWriteTime(path_newfolder);

            if (set_flag == 1)
            {
                source_Local_path_all1.Insert(0, new Local_path_all1()
                {
                    local_all_date = folderdate,
                    local_all_extension = "folder",
                    local_all_filename = str_newfolder,
                    local_all_size = "",
                    local_all_src = "Resources/icon_folder.png"
                });                                
                this.list_set1_local_all.Items.SortDescriptions.Clear();
                this.list_set1_local_all.Items.SortDescriptions.Add(new SortDescription("local_all_date", ListSortDirection.Descending));
                this.list_set1_local_all.Items.SortDescriptions.Add(new SortDescription("local_all_extension", ListSortDirection.Descending));
                this.list_set1_local_all.Items.SortDescriptions.Add(new SortDescription("local_all_filename", ListSortDirection.Ascending));
                this.list_set1_local_all.Items.Refresh();
            }
            else if (set_flag == 2)
            {
                source_Local_path_all2.Insert(0, new Local_path_all2()
                {
                    local_all_date = folderdate,
                    local_all_extension = "folder",
                    local_all_filename = str_newfolder,
                    local_all_size = "",
                    local_all_src = "Resources/icon_folder.png"
                });
                this.list_set2_local_all.Items.SortDescriptions.Clear();
                this.list_set2_local_all.Items.SortDescriptions.Add(new SortDescription("local_all_date", ListSortDirection.Descending));
                this.list_set2_local_all.Items.SortDescriptions.Add(new SortDescription("local_all_extension", ListSortDirection.Descending));
                this.list_set2_local_all.Items.SortDescriptions.Add(new SortDescription("local_all_filename", ListSortDirection.Ascending));
                this.list_set2_local_all.Items.Refresh();
            }            
        }
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,670 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
762 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Hui Liu-MSFT 38,191 Reputation points Microsoft Vendor
    2022-06-01T03:10:12.297+00:00

    Hi,@JunYoungLee . What is the code for your NewFolderInputBox , list_set1_local_all and uiList_textbox ? Is it possible to show the complete code that reproduces the problem for analysis?

    Possible solutions1:
    Use Insert instead of Add :

    collection.Insert(0, newItem);  
    

    Note that it's slower than Add since it has to shift all items by 1 position. It might be an issue if the list is very big.

    Possible solutions2:
    Adapt CustomSort for your needs:

    • You could give all new items an extreme value for the property used in IComparer object for sorting, e.G. int.MaxValue or string.Empty.
    • Or create an additional property IsNew and include it into comparing process.

    For more details, you could try and see if the solutions here work for you.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.