locked
Nest a ListView inside a Listview WPF RRS feed

  • Question

  •        

    I am trying to nest a listview inside a listview

    I have two classes:

    {
        public class TestData
        {
            public string Case { get; set; }
            public ObservableCollection<SubData> StainList { get; set; }
    
            public TestData()
            {
                StainList = new ObservableCollection<SubData>();
            }
        }

    And

    {
        public class SubData
        {
            public string Block { get; set; }
            public string Stain { get; set; }
    
        }
    }I am trying to display the data in the following XML page

                <ListView Name="listMain" ItemsSource="{Binding}">
                    <ListView.ItemTemplate>
                        <DataTemplate DataType="TestData">
                            <StackPanel>
                                <TextBlock Text="{Binding Case}"></TextBlock>
                                <StackPanel>
                                    <ListView Name="SubList" ItemsSource="{Binding Stains}">
                                        <ListView.ItemTemplate>
                                            <DataTemplate DataType="SubData">
                                                <TextBlock Text="{Binding Stain}"></TextBlock>
                                            </DataTemplate>
                                        </ListView.ItemTemplate>
                                    </ListView>
                                </StackPanel>
                            </StackPanel>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

    All I get is the TestData class displaying data. The sub listview seems to only display boxes

    Any help would be appreciated

    Thanks


    Thursday, February 11, 2016 1:48 AM

Answers

  • You have Stains as the ItemsSource for the embedded ListView but in your definition of TestData the collection is named StainList.

    Lloyd Sheen

    • Proposed as answer by Andy ONeill Thursday, February 11, 2016 12:31 PM
    • Marked as answer by detroit8420 Thursday, February 11, 2016 2:23 PM
    Thursday, February 11, 2016 8:19 AM

All replies

  • You have Stains as the ItemsSource for the embedded ListView but in your definition of TestData the collection is named StainList.

    Lloyd Sheen

    • Proposed as answer by Andy ONeill Thursday, February 11, 2016 12:31 PM
    • Marked as answer by detroit8420 Thursday, February 11, 2016 2:23 PM
    Thursday, February 11, 2016 8:19 AM
  • Thank you. I changed my code so many times trying to get this to work I don't know how I didn't make that change!
    Thursday, February 11, 2016 2:23 PM