locked
How to indent hierachical sub items of a collection to a listview and binding to it .

    Question

  • How can i bind and indent this type of class that has child elements and sub child elements etc  inside a listview ?

    Here is the type of class

    public class SubCategory { public string UniqueId { get; set; } public string Title { get; set; } public string Description { get; set; } public string ParentID { get; set; } public string indent { get; set; } public List<SubCategory> subCategory { get; set; } }

    Please guys i am stuck .



    19karabo91

    Monday, March 9, 2015 8:19 AM

Answers

  • XAML:

    <ListView x:Name="MyListView">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock Text="{Binding UniqueId}"/>
                            <ListView Margin="10 0 0 0" ItemsSource="{Binding subCategory}">
                                <ListView.ItemTemplate>
                                    <DataTemplate>
                                        <StackPanel Orientation="Horizontal">
                                            <TextBlock Text=" - "/>
                                            <TextBlock Text="{Binding UniqueId}"/>
                                        </StackPanel>
                                    </DataTemplate>
                                </ListView.ItemTemplate>
                            </ListView>
                        </StackPanel>                    
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
    Code:
    List<SubCategory> MyList = new List<SubCategory>();
                
    SubCategory A = new SubCategory();
    A.UniqueId = "A";
               
    
    SubCategory Aa = new SubCategory();
    Aa.UniqueId = "Aa";
    
    SubCategory Ab = new SubCategory();
    Ab.UniqueId = "Ab";
    
    SubCategory Ac = new SubCategory();
    Ac.UniqueId = "Ac";
    
    A.subCategory = new List<SubCategory>();
    A.subCategory.Add(Aa);
    A.subCategory.Add(Ab);
    A.subCategory.Add(Ac);
    
               
    SubCategory B = new SubCategory();
    B.UniqueId = "B";
                
    SubCategory C = new SubCategory();
    C.UniqueId = "C";
             
    SubCategory D = new SubCategory();
    D.UniqueId = "D";
    
    SubCategory Da = new SubCategory();
    Da.UniqueId = "Da";
    
    D.subCategory = new List<SubCategory>();
    D.subCategory.Add(Da);
              
    SubCategory E = new SubCategory();
    E.UniqueId = "E";
    
    SubCategory Ea = new SubCategory();
    Ea.UniqueId = "Ea";
    
    SubCategory Eb = new SubCategory();
    Eb.UniqueId = "Eb";
                
    E.subCategory = new List<SubCategory>();
    E.subCategory.Add(Ea);
    E.subCategory.Add(Eb);
    
    
    MyList.Add(A);
    MyList.Add(B);
    MyList.Add(C);
    MyList.Add(D);
    MyList.Add(E);
    MyListView.ItemsSource = MyList;



    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Tuesday, March 10, 2015 6:57 PM
    Moderator

All replies

  • Embed a listview inside a listview. The outer listview is bound to the collection of SubCategory objects, tThe inner listview is bound to collection of subcategory inside the outer SubCategory.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Monday, March 9, 2015 2:37 PM
    Moderator
  • I already tried that but it only bind the SbuCategory and not the child elements within subCategory , can you show me an example of how I can get this right ?

    19karabo91

    Monday, March 9, 2015 5:11 PM
  • XAML:

    <ListView x:Name="MyListView">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock Text="{Binding UniqueId}"/>
                            <ListView Margin="10 0 0 0" ItemsSource="{Binding subCategory}">
                                <ListView.ItemTemplate>
                                    <DataTemplate>
                                        <StackPanel Orientation="Horizontal">
                                            <TextBlock Text=" - "/>
                                            <TextBlock Text="{Binding UniqueId}"/>
                                        </StackPanel>
                                    </DataTemplate>
                                </ListView.ItemTemplate>
                            </ListView>
                        </StackPanel>                    
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
    Code:
    List<SubCategory> MyList = new List<SubCategory>();
                
    SubCategory A = new SubCategory();
    A.UniqueId = "A";
               
    
    SubCategory Aa = new SubCategory();
    Aa.UniqueId = "Aa";
    
    SubCategory Ab = new SubCategory();
    Ab.UniqueId = "Ab";
    
    SubCategory Ac = new SubCategory();
    Ac.UniqueId = "Ac";
    
    A.subCategory = new List<SubCategory>();
    A.subCategory.Add(Aa);
    A.subCategory.Add(Ab);
    A.subCategory.Add(Ac);
    
               
    SubCategory B = new SubCategory();
    B.UniqueId = "B";
                
    SubCategory C = new SubCategory();
    C.UniqueId = "C";
             
    SubCategory D = new SubCategory();
    D.UniqueId = "D";
    
    SubCategory Da = new SubCategory();
    Da.UniqueId = "Da";
    
    D.subCategory = new List<SubCategory>();
    D.subCategory.Add(Da);
              
    SubCategory E = new SubCategory();
    E.UniqueId = "E";
    
    SubCategory Ea = new SubCategory();
    Ea.UniqueId = "Ea";
    
    SubCategory Eb = new SubCategory();
    Eb.UniqueId = "Eb";
                
    E.subCategory = new List<SubCategory>();
    E.subCategory.Add(Ea);
    E.subCategory.Add(Eb);
    
    
    MyList.Add(A);
    MyList.Add(B);
    MyList.Add(C);
    MyList.Add(D);
    MyList.Add(E);
    MyListView.ItemsSource = MyList;



    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Tuesday, March 10, 2015 6:57 PM
    Moderator