c# - Validating ItemsControl on Button click in WPF -
i have itemscontrol item template contains 2 comboboxes. given item, sec combobox required iff first combobox has selected value. have set validation using idataerrorinfo on view model.
rather flagging combobox #2 invalid sec user selects value in combobox1, want perform validation when user tries save. it's kind of annoying have form "yell" @ doing wrong on field haven't had chance come in yet.
normally forcefulness validation retrieving bindingexpression combobox , calling updatesource() , determine if there error calling validation.gethaserror() passing combobox. since comboboxes generated dynamically itemscontrol, not easy to. have 2 questions: 1. how ensure validation has executed controls when save button clicked. 2. how check whether there validation errors when save button clicked. validation.gethaserror remains false itemscontrol when combobox2 within has error. thanks.
edit: had followed this article implement idataerrorinfo in order validate combobox properties relative each other.
public class introviewmodel : inotifypropertychanged, idataerrorinfo { public guid classscheduleid { { homecoming _intro.classscheduleid; } set { _intro.classscheduleid = value; onpropertychanged("classscheduleid"); //onpropertychanged("trialdate"); //this trigger validation on combobox2 when bound combobox1 changes } } public datetime trialdate { { homecoming _intro.trialdate; } set { _intro.trialdate = value; onpropertychanged("trialdate"); } } public string error { { homecoming null; } } public string this[string columnname] { { homecoming validateproperty(columnname); } } private string validateproperty(string propertyname) { string error = null; switch (propertyname) { case "trialdate": if (_intro.trialdate == datetime.minvalue && _intro.classscheduleid != guid.empty) error = "required"; break; default: error = null; break; } homecoming error; } }
i attempted create behavior need based on assumptions
sample
xaml
class="lang-xml prettyprint-override"><stackpanel> <stackpanel orientation="horizontal"> <button command="{binding additem}" content="add item" /> <button command="{binding save}" content="save" /> </stackpanel> <itemscontrol itemssource="{binding data}" grid.issharedsizescope="true"> <itemscontrol.itemtemplate> <datatemplate> <border x:name="border" borderthickness="1" padding="2" margin="2"> <grid> <grid.columndefinitions> <columndefinition sharedsizegroup="value1" /> <columndefinition /> </grid.columndefinitions> <combobox text="{binding value1}" itemssource="{binding source={staticresource sampledata}}" /> <combobox text="{binding value2}" itemssource="{binding source={staticresource sampledata}}" grid.column="1" /> </grid> </border> <datatemplate.triggers> <datatrigger binding="{binding isvalid}" value="false"> <setter targetname="border" property="borderbrush" value="red" /> </datatrigger> </datatemplate.triggers> </datatemplate> </itemscontrol.itemtemplate> </itemscontrol> </stackpanel>
main vm
class="lang-cs prettyprint-override"> public viewmodel() { additem = new simplecommand(i => data.add(new dataviewmodel(new datamodel()))); save = new simplecommand(i => { foreach (var vm in data) { vm.validateandsave(); } } ); info = new observablecollection<dataviewmodel>(); } public observablecollection<dataviewmodel> info { get; set; } public icommand additem { get; set; } public icommand save { get; set; }
data vm , model
class="lang-cs prettyprint-override">public class datamodel { public object value1 { get; set; } public object value2 { get; set; } } public class dataviewmodel : inotifypropertychanged { datamodel model; public dataviewmodel(datamodel model) { this.model = model; isvalid = true; } object _value1; public object value1 { { homecoming _value1; } set { _value1 = value; } } object _value2; public object value2 { { homecoming _value2; } set { _value2 = value; } } public bool isvalid { get; set; } public void validateandsave() { isvalid = !(_value1 != null && _value2 == null); propertychanged(this, new propertychangedeventargs("isvalid")); if (isvalid) { model.value1 = _value1; model.value2 = _value2; } } public event propertychangedeventhandler propertychanged; }
so vm validate items when click save , save items valid. otherwise mark isvalid property false notified ui
c# wpf validation data-binding itemscontrol
No comments:
Post a Comment