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:
<DataGridTemplateColumn Header="Path" Width="*" ItemsSource="{Binding Found_Items}" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DockPanel>
<CheckBox IsChecked="{Binding Path=Enabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</DockPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Then ensure you do this in the background:
public ObservableCollection<Item> Found_Items {get; set;}
For the changes of each item to be reflected you need to use INotifyPropertyChanged like so:
public class Item : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private bool enabled;
private BitmapImage itemIcon;
private string path;
private string size;
public string Size
{
get { return size; }
set
{
size = value;
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Size"));
}
}
public string Path
{
get { return path; }
set
{
path = value;
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Path"));
}
}
public BitmapImage ItemIcon
{
get { return itemIcon; }
set
{
itemIcon = value;
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("ItemIcon"));
}
}
public bool Enabled
{
get { return enabled; }
set
{
enabled = value;
if(PropertyChanged!=null) PropertyChanged(this,new PropertyChangedEventArgs("Enabled"));
}
}
}
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;
public class Item : INotifyPropertyChanged
{
private bool _Enabled;
private BitmapImage _ItemIcon;
private string _Path;
private string _Size;
public event PropertyChangedEventHandler PropertyChanged;
public Item(bool enabled, BitmapImage itemIcon, string path, string size)
{
_Enabled = enabled;
_ItemIcon = itemIcon;
_Path = path;
_Size = size;
}
public bool Enabled
{
get { return _Enabled; }
set
{
_Enabled = value;
this.NotifyPropertyChanged("Enabled");
}
}
public BitmapImage ItemIcon
{
get { return _ItemIcon; }
set
{
_ItemIcon = value;
this.NotifyPropertyChanged("ItemIcon");
}
}
public string Path
{
get { return _Path; }
set
{
_Path = value;
this.NotifyPropertyChanged("Path");
}
}
public string Size
{
get { return _Size; }
set
{
_Size = value;
this.NotifyPropertyChanged("Size");
}
}
private void NotifyPropertyChanged(string name)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
Everyting works perfect now.