0

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));
    }

}

2 Answers 2

2

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>
5
  • 1
    @meyou Try adding ItemsGrid.DataContext = this; to the constructor of your Window. It sounds like there's no DataContext set. You could also use a RelativeSource binding on the ItemsSource instead: ItemsSource="{Binding OrdersList, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" to tell the binding where to get the collection from. Oh, and make the OrdersList property public
    – Rachel
    Mar 21, 2012 at 12:52
  • Thanks, I solved it. Why does it work only if the property public? please explain it to me
    – Programer
    Mar 21, 2012 at 13:02
  • @meyou A public property is required for binding purposes. The binding system cannot access private properties or fields. From MSDN: The properties you use as binding source properties for a binding must be public properties of your class. Explicitly defined interface properties cannot be accessed for binding purposes, nor can protected, private, internal, or virtual properties that have no base implementation
    – Rachel
    Mar 21, 2012 at 13:06
  • Why would one want to put the entire code-behind of per example a MainWindow in the DataContext in DataContext = this? It just doesn't feel right, I would rather set the DataContext to the Collection and just set the ItemsSource as ItemsSource="{Binding}" Could you please explain why you advice it?
    – Silvermind
    Mar 21, 2012 at 19:09
  • @Silvermind Typically I would recommend the MVVM design pattern, which means the DataContext is a ViewModel object, and is not related to the code-behind at all. It doesn't look like the person posting this question was using the MVVM design pattern since the collection was a property of his Window object, so the simplest solution was to set the Window object as the DataContext
    – Rachel
    Mar 21, 2012 at 19:29
0

You need to set this.DataContext = this; somewhere. This is best done in the window's Load event.

1
  • 1
    How about itemsGrid.ItemsSource = this._ordersList? Mar 21, 2012 at 12:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.