c# - WPF DataGrid SelectAll Checkbox with two-way binding -
i'm binding datagrid list, has boolen property "isselected".
public partial class mainwindow : window { list<baselineentity> _blrecs; public mainwindow() { initializecomponent(); _blrecs = new list<baselineentity>(); _blrecs.add(new baselineentity{entityid = "yada_yada_1", isselected = false}); _blrecs.add(new baselineentity{entityid = "yada_yada_2", isselected = false}); _blrecs.add(new baselineentity{entityid = "yada_yada_2", isselected = false}); changedblrecsgridview.itemssource = _blrecs; } }
where class baselineentity defined as:
internal class baselineentity { public string entityid {get; set;} public bool isselected { get; set; } }
the xaml this
<datagrid name="changedblrecsgridview" itemssource="{binding _blrecs, mode=twoway, updatesourcetrigger=propertychanged}" autogeneratecolumns="false"> <datagrid.columns> <datagridtextcolumn header="entity id" binding="{binding entityid}"></datagridtextcolumn> <datagridtemplatecolumn width="30"> <datagridtemplatecolumn.celltemplate> <datatemplate> <checkbox ischecked="{binding path=isselected, updatesourcetrigger=propertychanged}" /> </datatemplate> </datagridtemplatecolumn.celltemplate> <datagridtemplatecolumn.header> <checkbox name="chkselectall" checked="chkselectall_checked" unchecked="chkselectall_unchecked"></checkbox> </datagridtemplatecolumn.header> </datagridtemplatecolumn> </datagrid.columns> </datagrid>
where event handlers checked , unchecked events of select-all checkbox this:
private void chkselectall_checked(object sender, routedeventargs e) { foreach (baselineentity enty in changedblrecsgridview.itemssource) { enty.isselected = true; } } private void chkselectall_unchecked(object sender, routedeventargs e) { foreach (baselineentity enty in changedblrecsgridview.itemssource) { enty.isselected = false; } }
i'm familiar mvvm , know haven't stuck best practices (like implementing viewmodel), cuz i'm getting started wpf.
the check , uncheck event handlers set , reset isselected property, view not getting updated change. looks intended 2-way binding isn't happening somehow.
my guess should have implemented baselineentity class interface push notifications view (this i'd expect working knowledge on knockout js, couldn't find after spending hours).
i don't know whatelse i'm missing here.. not find solution.
any help/guidance appreciated..
thank you!
refer below model code inotifypropertychanged
internal class baselineentity:inotifypropertychanged { public string entityid { get; set; } private bool isselected; public bool isselected { { return isselected; } set { isselected = value; onpropertychanged("isselected"); } } public event propertychangedeventhandler propertychanged; private void onpropertychanged(string propname) { if (propertychanged != null) { propertychanged(this, new propertychangedeventargs(propname)); } } }