Home > database >  Data Binding a List to an already bound object c#
Data Binding a List to an already bound object c#

Time:09-11

I have made a simple example to describe the problem

        <Grid>
            <StackPanel>
                <ListView x:Name="People" ItemsSource="{Binding}">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <TextBlock Text="{Binding Name}"/>
                                <ListView x:Name="PhoneNumbers" ItemsSource="{Binding PhoneNumbers}"/>
                            </StackPanel>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </StackPanel>
        </Grid>

So I have a ListView bound to an object let's call it Person() this object has a property of type collection that I want to access. I want to bind a list property in this case just a list of strings to x:name="PhoneNumber" how can I do this? I'm setting the ItemSource of the parent ListView in the code behind. I would like to know any information you have on this topic as I have not been able to find a whole lot on the subject read all of the c# docs, searched stack most I was able to find was to use an ObservableList which I tried, it didn't work. Looking for a data dump any information you think could get me moving in the right dirrection would be greatly appreciated as my actual use case is much more complicated than this example.

I'm aware this would be easier using MVVM but for now I want to get a basic prototype up and running without getting in to MVVM. The end goal is to move to MVVM.

Edit : Literally just binding in code behind atm

Simple example VV

    public LoadingWindow()
    {
        InitializeComponent();

        using (var db = new PeopleContext())
        {
            var Test = db.People.ToList();

            
            if (Test != null)
            {
                People.ItemsSource = Test;
            }
        }
    }

Example Class

public class People
{
    public string Name { get; set; } = "";

    // I did read that using an observable list was needed but I tried it and it didn't work
    // I'd prefer not to use an observable list
    public List<string> PhoneNumbers { get; set; } = new List<string>();
}

CodePudding user response:

Probably the question is not clear, but I figured it out. First of all, you should bind your ListView to ObservableCollection or BindingList in order to get UI updated when your collections are changed. The second problem is that you are not using property to bind your list, you are trying to set your ListView's ItemsSource property to the field, which is not possible. You are using this code var Test = DatabaseInfo.ToList(); Test is a field and then you are trying to bind to it, it is clear that it won't work. You should add a property to your MainWindow BindingList<Person>, and then in the MainWindow constructor fill it with information from DataBase.

So I just created a simple project, as I don't have DataBase I filled collections manually.

In MainWindow I have property BindingList<Person>

 public partial class MainWindow : Window
{
    public BindingList<Person> People { get; set; }
    public MainWindow()
    {
        InitializeComponent();

        People = new BindingList<Person>();

        Person person = new Person() { Name = "Jonh" };
        Person person2 = new Person() { Name = "Mike" };
        People.Add(person);
        People.Add(person2);

        PeopleList.ItemsSource = People;
    }
}

Person class is defined like this

public class Person
{
    public BindingList<string> PhoneNumbers { get; set; }
    public string Name { get; set; }
    public Person()
    {
        PhoneNumbers = new BindingList<string>();
        PhoneNumbers.Add("1");
        PhoneNumbers.Add("2");
        PhoneNumbers.Add("3");
        PhoneNumbers.Add("4");
    }
}

I used same XAML, your XAML code doesn't have any problems

<ListView x:Name="PeopleList">
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="{Binding Name}"/>
                    <ListView x:Name="PhoneNumbers" ItemsSource="{Binding PhoneNumbers}"/>
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

Here is result

enter image description here

Just use BindinList<T> or ObservableCollection<T> and it should work.

  • Related