0

I have a treeview showing folder structure (folders and files under).

I want to add a button to allow sorting the content (files) by Alphabetic order.

Here is my implementation:

XAML

<TreeView Name="WorkspaceTree" Grid.Row="1" FontSize="13" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" MinWidth="200" IsTextSearchEnabled="True" SelectedItemChanged="ProjectWorkspace_SelectedItemChanged" MouseRightButtonDown="ProjectWorkspace_MouseRightButtonDown" MouseDoubleClick="ProjectWorkspace_MouseDoubleClick">
   <TreeView.Resources>
      <ContextMenu x:Key ="FolderContext"  StaysOpen="true">
           <MenuItem x:Name="ProjectWorkspace_OpenAll" Header="Open All" Click="ProjectWorkspace_OpenAll_Click"/>
      </ContextMenu>
      <ContextMenu x:Key="ReaderContext"  StaysOpen="true">
           <MenuItem x:Name="ProjectWorkspace_Open" Header="Open" Click="ProjectWorkspace_Open_Click"/>
           <MenuItem x:Name="ProjectWorkspace_Build" Header="Build" Click="ProjectWorkspace_Build_Click"/>
      </ContextMenu>
   </TreeView.Resources>
   No workspace was loaded...
</TreeView>

The tree is filled in code behind. Here is a snapshot.

      ...

      WorkspaceTree.Items.Clear();
      var rootDirectoryInfo = new DirectoryInfo(dlg.FileName);
      TreeViewItem root_item = CreateDirectoryNode(rootDirectoryInfo);
      if (root_item != null)
      {
          WorkspaceTree.Items.Add(root_item);
          ...
}

private static TreeViewItem CreateDirectoryNode(DirectoryInfo directoryInfo)
{
     var directoryNode = new TreeViewItem { Header = directoryInfo.Name, Tag = "Folder" };
     foreach (var directory in directoryInfo.GetDirectories())
     {
         TreeViewItem dir_recursive_item = CreateDirectoryNode(directory);
         if (dir_recursive_item != null)
             directoryNode.Items.Add(dir_recursive_item);
         }

         foreach (var file in directoryInfo.GetFiles("*.txt"))
         directoryNode.Items.Add(new TreeViewItem { Header = file.Name, Tag = file.FullName });
         if (directoryNode.Items.Count == 0)
            return null;
         return directoryNode;
     }
}

Now after the tree is shown, by a UI interaction, its content should be sorted. I've place a button which its click event is:

private void Sort_Click(object sender, RoutedEventArgs e)
{
    WorkspaceTree.Items.SortDescriptions.Clear();
    WorkspaceTree.Items.SortDescriptions.Add(new SortDescription("Header", ListSortDirection.Ascending));
}

While the event is running - nothing happens on the Tree itself.

I event tried sorting over Header.Name in the SortDescription, with no change in behavior.

What am i missing?

NirMH
  • 4,769
  • 3
  • 44
  • 69
  • 1
    In WPF, you shouldn't populate the controls with data directly in code. Instead, you should create a view-model containing a collection of your data items and bind the control to that collection. Then, the sorting will work. – dymanoid Oct 29 '18 at 09:50
  • @dymanoid: how do i create a collection of hierarchical data... this is how i started in first place... – NirMH Oct 29 '18 at 09:55
  • There are **lots** of such questions and answers here on SO. Please do some research. E.g. look [here](https://stackoverflow.com/a/6560891/2846483). – dymanoid Oct 29 '18 at 09:58

0 Answers0