c# - WPF DataGrid Validation Bug? -
this may intended functionality, sure seems bug me.
i'm using out-of-the-box wpf datagrid, bound observablecollection , attempting use validation rules in order provide nice user feedback. needless there more issues can count, i'll stick immediate.
here summary of problem:
- bind
itemssource
propertyobservablecollection<t>
- populate collection
- edit item in grid in way cause validation error
- programatically remove item
observablecollection<t>
when these steps performed, gridview recognizes item has been removed collection, , removes row grid. however, the grid stuck in invalid state , no further actions can performed through ui on grid!
again, seems quite major bug me being able programmatically remove items collection kind of big deal.
has run this? suggestions how around it?
it worth noting have created separate solution isolate problem, answers questions might have:
does object implement inotifypropertychanged
? yes
is custom collection? no plain old observablecollection<t>
how removing items collection?
//find newly added item , remove var someobject = someobjects .where(obj => obj.someproperty == somevalue) .first(); someobjects.remove(someobject );
how binding validation rule?
<datagridtextcolumn header="someproperty"> <datagridtextcolumn.binding> <binding path="someproperty"> <binding.validationrules> <val:requiredvalidator validationstep="convertedproposedvalue" validatesontargetupdated="true" /> </binding.validationrules> </binding> </datagridtextcolumn.binding> </datagridtextcolumn>
what validation rule like?
public class requiredvalidator : validationrule { public override validationresult validate(object value, system.globalization.cultureinfo cultureinfo) { if (value == null || string.isnullorwhitespace(value string)) return new validationresult(false, "field required!"); return validationresult.validresult; } }
i had same problem , after long search process found solution:
you can create class derived datagrid. there can access private properties reflection. if remove invalid item can call function setgridwritable() , other values editable again.
public class mydatagrid : datagrid { public void setgridwritable() { bindingflags bindingflags = bindingflags.flattenhierarchy | bindingflags.nonpublic | bindingflags.instance; propertyinfo cellerrorinfo = this.gettype().basetype.getproperty("hascellvalidationerror", bindingflags); propertyinfo rowerrorinfo = this.gettype().basetype.getproperty("hasrowvalidationerror", bindingflags); cellerrorinfo.setvalue(this, false, null); rowerrorinfo.setvalue(this, false, null); } }
Comments
Post a Comment