0

I'm new to C# WPF and was trying to create a List within a List. I have a List of Person and each Person have a List of Friends. I was able to do this using ListView within a ListView. I am trying to create a Button for every friend and display the Name of the Friend through a MessageBox but I do not know how.

This is my ListView within a ListView

<ListView ItemsSource="{Binding people}" x:Name="ListA">
        <ListView.ItemTemplate>
            <DataTemplate>
                <TabItem Header="{Binding Name}">
                    <Grid>
                        <ListView x:Name="ListB" ItemsSource="{Binding Friends}" Margin="100,0,0,0">
                             <ListView.ItemTemplate>
                                <DataTemplate>
                                    <Grid>
                                        <Button Click="Button_Click" Content="Show Name" Margin="0,0,0,100"/>
                                        <TextBlock Text="{Binding Name }"/>
                                    </Grid>
                                </DataTemplate>
                             </ListView.ItemTemplate>
                        </ListView>
                    </Grid>
                </TabItem>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

This is my xaml.cs

public List<Person> people { get; set; }
    public MainWindow()
    {
        InitializeComponent();
        
        people = new List<Person>();

        

        Person person1 = new Person();
        person1.Name = "Mark";
        person1.Friends = new List<Friend>();
        person1.Friends.Add(new Friend("Friend A"));
        person1.Friends.Add(new Friend("Friend B"));
        person1.Friends.Add(new Friend("Friend C"));

        Person person2 = new Person();
        person2.Name = "John";
        person2.Friends = new List<Friend>();
        person2.Friends.Add(new Friend("Friend D"));
        person2.Friends.Add(new Friend("Friend E"));
        person2.Friends.Add(new Friend("Friend F"));

        Person person3 = new Person();
        person3.Name = "Peter";
        person3.Friends = new List<Friend>();
        person3.Friends.Add(new Friend("Friend G"));
        person3.Friends.Add(new Friend("Friend H"));
        person3.Friends.Add(new Friend("Friend I"));

        people.Add(person1);
        people.Add(person2);
        people.Add(person3);

        DataContext = this;

        
    }

I can access ListA in xaml.cs but not ListB and I do not know why. I may be wrong in approaching the problem but I dont really know anyway how.

2

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.