john doe
john doe

Reputation: 9660

WPF DataGrid Not Binding to Collection

For some reason my DataGrid which is contained in a WPF UserControl is not binding to a collection. It always comes out with headers only and none of the rows for the content are added.

Users.xaml.cs:

 public partial class Users : UserControl
    {
        private ObservableCollection<UserViewModel> _userViewModels;

        public ObservableCollection<UserViewModel> UserViewModels
        {
            get { return _userViewModels; }
            set { _userViewModels = value;  }
        }

        public Users()
        {
            InitializeComponent();
            PopulateUsers(); 
        }      

        private void PopulateUsers()
        {
            _userViewModels = new ObservableCollection<UserViewModel>() 
            {
                new UserViewModel() { FirstName = "Mary", LastName = "Doe"}, 
                new UserViewModel() { FirstName = "John", LastName = "Doe"}
            };

            this.DataContext = _userViewModels; 
        }


    }

Users.xaml:

<UserControl x:Class="Users.Views.Users"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">

   <DataGrid ItemsSource="{Binding Source=UserViewModels}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="First Name" Binding="{Binding FirstName}"></DataGridTextColumn>

            <DataGridTextColumn Header="Last Name"></DataGridTextColumn>           
        </DataGrid.Columns>
    </DataGrid>

</UserControl>

Can anyone notice anything I am missing?

UPDATE 1:

I updated the XAML code to the following:

<DataGrid ItemsSource="{Binding Source=UserViewModels}" AutoGenerateColumns="False">

Now, I can see the DataGrid but it does not have any values. It is all empty! Even though I am populating two fields "FirstName" and "LastName".

Upvotes: 0

Views: 159

Answers (3)

d.moncada
d.moncada

Reputation: 17402

You need to update your bindings and set the DataContext of the UserControl. This can be done strictly in xaml as shown below.

xaml:

<UserControl x:Class="Users.Views.Users"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
             DataContext="{Binding RelativeSource={RelativeSource Self}}">
   <DataGrid ItemsSource="{Binding UserViewModels}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="First Name" Binding="{Binding FirstName}"/>
            <DataGridTextColumn Header="Last Name" Binding="{Binding LastName}"/>        
        </DataGrid.Columns>
    </DataGrid>
</UserControl>

and remove this line:

this.DataContext = _userViewModels; 

The above code will set the current DataContext of the UserControl to itself. When this is applied, you are able to bind accordingly, as shown above. Note that your XAML did not have anything binding to your Last Name column, so I added that as well.

Upvotes: 0

123 456 789 0
123 456 789 0

Reputation: 10865

You are doing it wrong. Also, binding is more useful if you have ViewModel.

The reason why it didn't work the first time is because you were trying to set the entire view as the DataContext. Therefore, the binding was trying to look for a property of UserViewModels inside the UserViewModels.

Change

this.DataContext = _userViewModels; 

to

this.DataContext = this; 

and put the bindings back what it originally is

ItemsSource="{Binding UserViewModels}" 

Upvotes: 1

dkozl
dkozl

Reputation: 33364

Change ItemsSource binding to

ItemsSource="{Binding}"

DataContext, for whole UserControl, is already set to your collection when you do

this.DataContext = _userViewModels

so all you need to do is bind ItemsSource to current DataContext

Upvotes: 0

Related Questions