c# - WPF DataGrid - Modifing Visuals out of scroll view at runtime -
i have multiple datagrids first column has checkbox , header has checkbox. we've seen, want able click header checkbox , have select of check boxes below.
<datagrid x:name="dgreporttypes_new" margin="10,140,0,0" height="175" verticalalignment="top" horizontalalignment="left" width="216" autogeneratecolumns="false" selectionmode="single" canuseraddrows="false" mouseup="datagrid_mouseup" keydown="datagrid_keydown"> <datagrid.columns> <datagridcheckboxcolumn isreadonly="true" binding="{binding ischecked, updatesourcetrigger=propertychanged}" clipboardcontentbinding="{x:null}"> <datagridcheckboxcolumn.headertemplate > <datatemplate> <checkbox x:name="chkselectall" isthreestate="true" checked="datagridheader_checked" unchecked="datagridheader_unchecked" indeterminate="datagridheader_indeterminate" /> </datatemplate> </datagridcheckboxcolumn.headertemplate> </datagridcheckboxcolumn> <datagridtextcolumn isreadonly="true" binding="{binding description}" canuserresize="false" width="*" /> </datagrid.columns> </datagrid>
checked
private void datagridheader_checked(object sender, routedeventargs e) { datagrid dg = sgt.helpers.controlhelpers.generics.findvisualparent<datagrid>(sender dependencyobject, typeof(datagrid)); (int = 0; < dg.items.count; i++) { datagridrow row = (datagridrow)dg.itemcontainergenerator.containerfromindex(i); checkbox c = controlhelpers.checkboxes.togglecheckbox(row, true); } e.handled = true; }
unchecked
private void datagridheader_unchecked(object sender, routedeventargs e) { datagrid dg = sgt.helpers.controlhelpers.generics.findvisualparent<datagrid>(sender dependencyobject, typeof(datagrid)); (int = 0; < dg.items.count; i++) { datagridrow row = (datagridrow)dg.itemcontainergenerator.containerfromindex(i); checkbox c = controlhelpers.checkboxes.togglecheckbox(row, false); } e.handled = true; }
indeterminate
private void datagridheader_indeterminate(object sender, routedeventargs e) { ((checkbox)sender).ischecked = false; e.handled = true; }
toggle
public static checkbox togglecheckbox(datagridrow row, bool? overridevalue) { if (row == null) { return null; } checkbox c = generics.findvisualchild<checkbox>(row dependencyobject, typeof(checkbox)); if (overridevalue != null) { c.ischecked = overridevalue; } else { c.ischecked = !c.ischecked; } return c; } public static checkbox togglecheckbox(object sender, mousebuttoneventargs e, bool? overridevalue) { datagridrow row = itemscontrol.containerfromelement((datagrid)sender, e.originalsource dependencyobject) datagridrow; if (row == null) { return null; } checkbox c = generics.findvisualchild<checkbox>(row dependencyobject, typeof(checkbox)); if (overridevalue != null) { c.ischecked = overridevalue; } else { c.ischecked = !c.ischecked; } e.handled = true; return c; }
find visual
public static t findvisualchild<t>(dependencyobject parent, type type) t : dependencyobject { if (parent == null) debug.print("parent null"); (int = 0; < visualtreehelper.getchildrencount(parent); i++) { var child = visualtreehelper.getchild(parent, i); if (child.gettype() == type) { debug.print(child.tostring()); return child t; } else { t result = findvisualchild<t>(child, type); if (result != null) return result; } } return null; }
datagrid clicked
private void datagrid_mouseup(object sender, mousebuttoneventargs e) { checkbox c = controlhelpers.checkboxes.togglecheckbox(sender, e, null); validatecheckall((datagrid)sender); }
handle header checkbox when items checked/unchecked
private void validatecheckall(datagrid grid) { //headeredcontentcontrol datagridcolumnheaderspresenter hp = helpers.controlhelpers.generics.findvisualchild<datagridcolumnheaderspresenter>(grid, typeof(datagridcolumnheaderspresenter)); checkbox c = helpers.controlhelpers.generics.findvisualchild<checkbox>(hp, typeof(checkbox)); c.checked -= new routedeventhandler(datagridheader_checked); c.unchecked -= new routedeventhandler(datagridheader_unchecked); c.indeterminate -= new routedeventhandler(datagridheader_indeterminate); int counter = 0; (int = 0; < grid.items.count; i++) { datagridrow row = (datagridrow)grid.itemcontainergenerator.containerfromindex(i); //row.item if (row != null) { checkbox ch = helpers.controlhelpers.generics.findvisualchild<checkbox>(row, typeof(checkbox)); if (ch.ischecked == true) { counter++; } } debug.print(counter.tostring() + " of " + grid.items.count.tostring()); } if (counter == 0) { c.ischecked = false; } else if (counter == grid.items.count) { c.ischecked = true; } else { c.ischecked = null; } c.checked += new routedeventhandler(datagridheader_checked); c.unchecked += new routedeventhandler(datagridheader_unchecked); c.indeterminate += new routedeventhandler(datagridheader_indeterminate); }
i have working way expected, until... got datagrid had scrollable data.
problem: visible checkboxes change. i'm using visuals checkbox , understand why happening.
question: there way change rest of checkboxes out of view?
i changed way works , added ischecked data behind. looped through data , chanced ischecked , refreshed grid
Comments
Post a Comment