Search code examples
xamlc#-4.0windows-store-apps

How to bind data the inner list view in Windows Store apps


Here is my code for binding data to the list view , unable to bind data to the inner list view written in windows store app.

Please check the out put in attached image.

ManiPage.xaml.cs:

enter image description here

public  class testchild
{
  public string data { get; set; }
}

 public class Test
  {
   public string id { get; set; }
   public string name { get; set; }
   public List<testchild> list { get; set; }
  }

  List<Test> listoftest = new List<Test>()
        {
            new Test()
            {
                id = "4218",
                name = "srujana",
                list = new List<testchild>()
                {
                   new testchild()
                   {
                       data = "hello"
                   }
                }
 },
  new Test()
{
id = "c7110",
name = "chandu",
list = new List<testchild>()
                {
                   new testchild()
                   {
                       data = "hello"
                   }
                }
  }
        };
        lstbx.ItemsSource = listoftest;

xaml:

 <ListView Height="300" Width="300" Name="lstbx">
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="{Binding id}"/>
                    <TextBlock Text="{Binding name}"/>
                    <ListView  ItemsSource="{Binding list}">
                        <TextBlock Text="{Binding data}"/>
                    </ListView>
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

Solution

  • You were quite close to the correct answer already, you just need to put the inner TextBlock in an ItemTemplate as well, similar as you did for the outer ListView.

    <ListView Height="300" Width="300" Name="lstbx">
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="{Binding id}"/>
                    <TextBlock Text="{Binding name}"/>
                    <ListView  ItemsSource="{Binding list}">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding data}"/>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    

    This results in:

    enter image description here