For some reasons I cannot seem to populate a datagrid I get the following error:
System.Windows.Data Error: 40 : BindingExpression path error: ‘Customers’ property not found on ‘object’ ”ObservableCollection`1′ (HashCode=11497055)
Can you spot what I am doing wrong?
The code is as follows:
ViewModel
=============
public class CustomerVM : ViewModelBase
{
private ObservableCollection<Customer> _customers;
public ObservableCollection<Customer> Customers
{
get { return _customers; }
set
{
_customers = value;
OnPropertyChanged("Customers");
}
}
private DelegateCommand _getCustomersCommand;
public ICommand GetCustomersCommand
{
get
{
return _getCustomersCommand ?? (_getCustomersCommand = new DelegateCommand(x => GetCustomers(), x => CanGetCustomer));
}
}
private static bool CanGetCustomer
{
get { return true; }
}
private void GetCustomers()
{
Customers = new ObservableCollection<Customer>
{
new Customer{Id=1,Name ="Jo",Surname = "Bloggs",IsMember = true},
new Customer{Id=2,Name ="Mark", Surname = "Cole",IsMember = false},
new Customer{Id=3,Name ="Robert ", Surname = "Smith",IsMember = true},
new Customer{Id=4,Name ="Vincent", Surname = "Varc",IsMember = false},
};
}
}
Customer Wpf Window
====================
<Window x:Class="Sample.CustomerDataGridSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
Title="CustomerDataGridSample" Height="300" Width="300" Loaded="OnWindowsLoaded" WindowStartupLocation="CenterScreen">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Text="Customers"
Grid.ColumnSpan="2"
FontWeight="Bold"
FontSize="12"/>
<toolkit:DataGrid x:Name="dg" Grid.Row="1"
ItemsSource="{Binding Customers}"
CanUserAddRows="False"
CanUserDeleteRows="False"
CanUserResizeRows="False"
CanUserSortColumns="False"
AutoGenerateColumns="False"
RowHeaderWidth="17" RowHeight="25">
<toolkit:DataGrid.Columns>
<toolkit:DataGridTextColumn Header="Name"
Binding="{Binding Path=Name}" />
<toolkit:DataGridTextColumn Header="Surname"
Binding="{Binding Path=Surname}" />
<toolkit:DataGridTextColumn Header="Is Member"
Binding="{Binding Path=IsMember}" />
</toolkit:DataGrid.Columns>
</toolkit:DataGrid>
</Grid>
</Window>
public partial class CustomerDataGridSample:Window
{
public CustomerDataGridSample()
{
InitializeComponent();
}
private void OnWindowsLoaded(object sender, RoutedEventArgs e)
{
var customerVm = new CustomerVM();
customerVm.GetCustomersCommand.Execute(null);
DataContext = customerVm.Customers;
}
}
The error message alone gives it away – your binding has the wrong context.
You’re setting the
DataContextof your window tocustomerVm.Customersin the loaded method. That means that your bindings “start” from theCustomersproperty already, and so the binding on your grid is actually trying to findcustomerVm.Customers.Customers. That’s why you get the error message you do: anObservableCollectionhas noCustomersproperty.You can either change it so the
DataContextset in code is simplycustomerVm, or useItemsSource={Binding}on the DataGrid (which will then just pick up the currentDataContext).