I am a newbie to WPF so please excuse my ignorance. I have read online that inorder for binding to work, the observable collection should be a public property. So, here's what I have done so far:
I have a class in which I have declared the observable like this:
public ObservableCollection<InventoryDto> invCollection { get; set; }
public InventoryDto()
{
invCollection = new ObservableCollection<InventoryDto>();
}
public ObservableCollection<InventoryDto> observableList
{
get
{
return this.invCollection;
}
set
{
invCollection = value;
OnPropertyChanged("observableList");
}
}
Then, in my view, I have a binding like this:
ItemsSource="{Binding observableList,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
In the view's code behind (i.e, view.xamls.cs), I am populating the observable:
foreach (var item in inventoryList)
{
invDto.invCollection.Add(new InventoryDto { location = item.location.Name, onHand = item.OnHand.ToString(), committed = item.committed.ToString() });
}
However, I dont see any data in my grid unless I add this code:
InventoryDataGrid.ItemsSource = invDto.observableList;
First, I find it odd to add this code in code behind whereas I have already specified ItemsSource in xaml. Second, this code populates the grid. But when I remove a row from db, the row does not get removed in the grid although I have specified BindingMode = TwoWay.
Please let me know what I am missing here.