Reputation: 1035
Please help me, I have no idea whats wrong. No matter what I try, the grid is just not updated (stays empty).
I want the grid to be bound to an ObservableCollection, but not to genrate automatic cloumns, but to choose two Properties from the object called Product, which is the type this Collection holds.
XAML:
<DataGrid x:Name="itemsGrid" ItemsSource="{Binding OrdersList}" AutoGenerateColumns="False" Style="{StaticResource GridStyle}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=Product.Amount}" Header="AMOUTN" />
<DataGridTextColumn Binding="{Binding Path=Product.Name}" Header="NAME" />
</DataGrid.Columns>
</DataGrid >
CODE:
public partial class Orders : Window,INotifyPropertyChanged
{
ObservableCollection<Product> _ordersList = new ObservableCollection<Product>();
public event PropertyChangedEventHandler PropertyChanged;
private ObservableCollection<Product> OrdersList
{
get { return this._ordersList; }
set { _ordersList = value; NotifyPropertyChanged("OrdersList"); }
}
private void addProduct(Product p)
{
OrdersList.Add(p);
NotifyPropertyChanged("OrdersList");
}
private void removeProduct(Product p)
{
OrdersList.Remove(p);
NotifyPropertyChanged("OrdersList");
}
protected void NotifyPropertyChanged(String propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
Upvotes: 0
Views: 5176
Reputation: 56697
You need to set this.DataContext = this;
somewhere. This is best done in the window's Load
event.
Upvotes: 0
Reputation: 132548
I think you simply need to remove the word Product.
from your bindings. The DataContext
of each DataGridRow
is an object of type Product
, so your binding should point to the properties on Product
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=Amount}" Header="AMOUNT" />
<DataGridTextColumn Binding="{Binding Path=Name}" Header="NAME" />
</DataGrid.Columns>
Upvotes: 2