0

I want to implement a sidebar as a UserControl in WPF.I want to load the content of the sidebar dynamically from the database. I can add any menu i can remove any, i can change the level of nesting from database. is there a way to do that with XAML and C# in WPF.

My class for the menu looks something like this

public class Menus
  {
    public string MenuName {get;set;}
    public string MenuURL {get;set;}
    public List<Menus> Children {get;set;}

    public Menus()
      {
        Children = new List<Menus>();
      }
  }

I have tried various combinations with and but no luck.

the output should look something like this.

  • Main Menu 1

    • Sub Menu 1.1
    • Sub Menu 1.2
      • Sub Menu 1.2.1
  • Main Menu 2

    • Sub Menu 2.1
      • Sub Menu 2.2

thus i would be able to add any number of nested behavior from the database. I am fairly new to WPF so i don't know many ways to this. if any straight forward way is there to accomplish this then that would help

rcode
  • 23
  • 5
  • As for view, you can use `TreeView` with `HierarchicalDataTemplate`. – emoacht Apr 06 '21 at 13:13
  • I have tried treeview which accomplishes the dynamic nesting part but it surely doesn't look anything like a "Sidebar". And another thing is i want to control the expanding and collapsing of the nodes on closing of the sidebar; but treeview doesn't have isExpander property per se. TreeviewItem does have it but i have no idea about how to use it with treeview and still get the desired results. – rcode Apr 07 '21 at 04:34
  • As for appearance of TreeView, you need to customize Style of TreeView, TreeViewItem and related controls. As for expanding/collapsing, you can use `TreeViewItem.IsExpanded` property. – emoacht Apr 08 '21 at 01:00

0 Answers0