How do I set a DataTemplate for a WPF TreeView to display all Elements of an List?

前端 未结 3 1253
清歌不尽
清歌不尽 2020-12-20 22:15

I\'d like to visualize the following data structure using TreeViews in WPF:

class MyDataContext
{
    ICollectionView Outers {get;set;}
    //...
}

class Ou         


        
3条回答
  •  我在风中等你
    2020-12-20 23:06

    Maybe this helps you a little bit ;)

    The xaml:

        
            
                
                    
                        
                            
                                

    The data:

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
    
            DataContext = new MyDataContext();
        }
    }
    
    
    
        class MyDataContext
        {
            public ObservableCollection Outers { get; set; }
    
    
            public MyDataContext()
            {
                Outers = new ObservableCollection();
                Outers.Add(new Outer() { Name = "Herp" });
                Outers.Add(new Outer() { Name = "Derp" });
            }
        }
    
        class Outer
        {
            public string Name { get; set; }
            public ObservableCollection Actions { get; set; }
    
            public Outer()
            {
                Actions = new ObservableCollection();
                Actions.Add(new Inner { Name = "Test1" });
                Actions.Add(new Inner { Name = "Test2" });
                Actions.Add(new Inner { Name = "Test3" });
                Actions.Add(new Inner { Name = "Test4" });
                Actions.Add(new Inner { Name = "Test5" });
                Actions.Add(new Inner { Name = "Test6" });
                Actions.Add(new Inner { Name = "Test7" });
            }
        }
    
    
        class Inner
        {
            public string Name { get; set; }
            public ICommand OnClick { get; set; }
        }
    

    And if you are using Commands... Try it with this example: ICommand

提交回复
热议问题