I have an ObservableCollection like this,
ObservableCollection<Item> Found_Items = new ObservableCollection<Item>();
public struct Item
{
public bool Enabled { get; set; }
public BitmapImage ItemIcon { get; set; }
public string Path { get; set; }
public string Size { get; set; }
}
I'm setting Datagrid's itemsource like this,
FoundItemsDatagrid.ItemsSource = Found_Items;
I have a checkbox in Datagrid like this,
<DataGridTemplateColumn Header="Path" Width="*" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DockPanel>
<CheckBox IsChecked="{Binding Path=Enabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</DockPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
I want, whenever i check or uncheck the checkbox on datagrid it should update my ObservableCollection.
What is the easiest way to do this ?
Thanks..
The problem is how you are binding to your collection. You are setting the ItemsSource explicitly therefore the ObservableCollection will not work the way you want it to.
Instead use binding like so:
Then ensure you do this in the background:
For the changes of each item to be reflected you need to use INotifyPropertyChanged like so:
Now when an item is changed by the users the change can be seen in the ObservableCollection. This is thanks to that INotifyPropertyChanged.
I followed the instructions HERE.
I changed "Item" struct to "Item" class like this;
Everyting works perfect now.