Auroth21
Auroth21

Reputation: 5

C# WPF MVVM cannot show elements of list

Im trying to write a simple WPF app with MVVM pattern, but showing elements of a list does not work I ame pretty sure that something is wrong with binding because its my first time with it

 <Window.Resources>
        <local:ViewModel x:Key="test"/>
    </Window.Resources>
    <Grid>
        <ListView Name="lstPersons" ItemsSource="{Binding test.peopleList}" >
            <ListView.View>
                    <GridView.Columns>
                        <GridViewColumn Header="name" DisplayMemberBinding="{Binding name}" />
                        <GridViewColumn Header="surname" DisplayMemberBinding="{Binding surname}" />

View Model fragment:

public class ViewModel
{
    private personModel.Root peopleDB = new personModel.Root();
    public ViewModel()
    { }

    public List<personModel.Person> peopleList
    {
        get { return peopleDB.people; }
    }

Model class fragment:

  public class Root
    {
        public List<Person> people;

        public Root()
        {
            people = new List<Person>();
            people.Add(new Person("aa", "aa", 1, new Adress("bb", "cc")));
            people.Add(new Person("bb", "bb", 1, new Adress("bb", "cc")));
            people.Add(new Person("cc", "cc", 1, new Adress("bb", "cc")));
        }
    }


    public class Person
    {
        public string name { get; set; }
        public string surname { get; set; }
        public int age { get; set; }
        public Adress address { get; set; }

tried couple of things with binding but none of them worked :/

Upvotes: 0

Views: 896

Answers (2)

Rachel
Rachel

Reputation: 132548

The problem here sounds like your DataContext is not set.

There's multiple ways of doing that. As escull638 said, you could manually hardcode the DataContext in with the Window using either XAML

<Window.DataContext>
   <local:ViewModel>
</Window.DataContext>

or Code-Behind

this.DataContext = new ViewModel();

and change your binding now that the .DataContext is set correctly

<ListView ItemsSource="{Binding peopleList}">

But keep in mind that hardcoding the .DataContext like this is typically only be used at the highest level in your application, and it should not be something common to see when working with WPF. Controls in WPF are intentially "lookless", and the binding system is used to pass them their data, so by doing something like hardcoding the DataContext means you cannot use the control with any other data object, which kind of defeats one of the biggest advantages of using WPF.

Another solution would be to change the Source property of your binding so it points to the static object defined in <Window.Resources>

<ListView ItemsSource="{Binding Source={StaticResource test}, Path=peopleList}">

I prefer this way because it's obvious just looking at the ListView XAML that you are binding to a static source, and it saves all kind of headaches later when you're trying to pass a dynamic source into the Control and discovering the DataContext isn't set to what you expect it.

As a side note, if you're having trouble understanding what the DataContext is for or how it works, I tend to link beginners to this answer of mine which explains it in more detail :)

Upvotes: 1

Eamon Scullion
Eamon Scullion

Reputation: 1384

Set the DataContext to the viewmodel by adding this to your xaml file:

<Window.DataContext>
   <local:ViewModel>
</Window.DataContext>

Then when you need to bind something you can just use:

<ListView Name="lstPersons" ItemsSource="{Binding peopleList}" >

Upvotes: 1

Related Questions