c# - ComboBox SelectedValuePath Validation not working -
i trying implement data validation in xaml / wpf first time. have read several tutorials , there seems variety of ways it. have settled on 1 approach , not working.
first, ideally how error displayed (above combobox
, right of label
). if possible ...
i created validationrule
:
using system; using system.globalization; using system.windows.controls; namespace oclmeditor.validationrules { class studypointvalidationrule : validationrule { public bool biblereading { get; set; } public studypointvalidationrule() { biblereading = false; } public override validationresult validate(object value, cultureinfo cultureinfo) { if(value == null) return new validationresult(false, "the study point has not been set."); int istudy = (int)value; if(biblereading) { if(istudy > 17) return new validationresult(false, "maximum study point bible readings 17."); } else { if(istudy == 7 || istudy > 51) return new validationresult(false, "study points 7, 52 , 53 not permitted student items."); } return new validationresult(true, null); } } }
then, added window
class in xaml:
xmlns:validationrules="clr-namespace:oclmeditor.validationrules":
in window.resources
:
<controltemplate x:key="studypointvalidationtemplate"> <dockpanel> <textblock foreground="red" fontsize="20">!</textblock> <adornedelementplaceholder/> </dockpanel> </controltemplate>
then, first combobox want validation applied changed from:
<combobox datacontext="{binding datacontext, elementname=oclmeditor}" itemssource="{binding readingstudypointslist}" itemcontainerstyle="{staticresource studypointcomboboxstyle}" itemtemplate="{staticresource studypointcomboitem}" validation.errortemplate="{staticresource studypointvalidationtemplate}" tag="{binding meeting.biblereadingmainname, mode=oneway, updatesourcetrigger=propertychanged}" selectedvalue="{binding meeting.biblereadingmainstudypoint, mode=twoway, updatesourcetrigger=propertychanged}" selectedvaluepath="number"/>
to:
<combobox datacontext="{binding datacontext, elementname=oclmeditor}" itemssource="{binding readingstudypointslist}" itemcontainerstyle="{staticresource studypointcomboboxstyle}" itemtemplate="{staticresource studypointcomboitem}" validation.errortemplate="{staticresource studypointvalidationtemplate}" tag="{binding meeting.biblereadingmainname, mode=oneway, updatesourcetrigger=propertychanged}" selectedvalue="{binding meeting.biblereadingmainstudypoint, mode=twoway, updatesourcetrigger=propertychanged}" > <combobox.selectedvaluepath> <binding path="number"> <binding.validationrules> <validationrules:studypointvalidationrule biblereading="true"/> </binding.validationrules> </binding> </combobox.selectedvaluepath> </combobox>
i error:
system.windows.data error: 40 : bindingexpression path error: 'number' property not found on 'object' ''oclmeditorviewmodel' (hashcode=17586541)'. bindingexpression:path=number; dataitem='oclmeditorviewmodel' (hashcode=17586541); target element 'combobox' (name=''); target property 'selectedvaluepath' (type 'string')
i don't understand why because able use "number" previously.
update
i have extended model data classes throw exception if value high. example:
[xmlignore] public int biblereadingmainstudypoint { { return _tfgw.biblereadingitem.main.studypoint; } set { if (value > 17) throw new argumentoutofrangeexception("maximum bible reading study point 17."); _tfgw.biblereadingitem.main.studypoint = value; markdirty(); onpropertychanged("biblereadingmainstudypoint"); } }
i definately exception thrown:
parameter name: maximum bible reading study point 17. @ oclmeditor.data.meetinginfo.meeting.set_biblereadingmainstudypoint(int32 value) in d:\my programs\oclmeditor\oclmeditor\data\meetinginfo\meeting.cs:line 234 @ oclmeditor.oclmeditorviewmodel.set_selectedstudentitem(student value) in d:\my programs\oclmeditor\oclmeditor\viewmodels\oclmeditorviewmodel.cs:line 184'
and changed xaml since validationrule not apply in instance:
<binding.validationrules> <exceptionvalidationrule/> </binding.validationrules>
so expecting show error visually. no nothing on screen.
the resources:
<window.resources> <valueconverters:studypointworkingon x:key="studypointworkingon" /> <style x:key="studypointcomboboxstyle" targettype="comboboxitem"> <setter property="tag" value="{binding number}" /> <style.triggers> <datatrigger value="true"> <datatrigger.binding> <multibinding converter="{staticresource studypointworkingon}"> <binding relativesource="{relativesource self}" path="tag"/> <binding path="datacontext" elementname="oclmeditor" updatesourcetrigger="propertychanged" /> <binding path="tag" relativesource="{relativesource ancestortype=combobox}"/> </multibinding> </datatrigger.binding> <setter property="background" value="red"/> </datatrigger> </style.triggers> </style> <datatemplate x:key="studypointcomboitem"> <stackpanel orientation="horizontal"> <textblock text="{binding number}"/> <textblock text=" - "/> <textblock text="{binding title}"/> </stackpanel> </datatemplate> <controltemplate x:key="studypointvalidationtemplate"> <dockpanel> <textblock foreground="red" fontsize="20">!</textblock> <adornedelementplaceholder/> </dockpanel> </controltemplate> </window.resources>
update 2
i can make out there validation symbol on teh screen. has not reduced width of combo can seen.
selectedvaluepath
string. tells combobox
value of selected data item assign selectedvalue
property. there's nothing validate that; that's not property tells user selected. shouldn't change when selection changes, because number
property want use selectedvalue
(unless change type of items in combobox
-- that's not you're doing).
the property want bind validation selectedvalue
, you've got attribute in question.
<combobox ...other properties... selectedvaluepath="number" > <combobox.selectedvalue> <binding path="meeting.biblereadingmainstudypoint" mode="twoway" updatesourcetrigger="propertychanged" > <binding.validationrules> <validationrules:studypointvalidationrule biblereading="true"/> </binding.validationrules> </binding> </combobox.selectedvalue> <combobox.style> <style targettype="combobox" basedon="{staticresource {x:type combobox}}" > <style.triggers> <trigger property="validation.haserror" value="true"> <setter property="tooltip" value="{binding relativesource={relativesource self}, path=(validation.errors)[0].errorcontent}"/> </trigger> </style.triggers> </style> </combobox.style> </combobox>
Comments
Post a Comment