c# - WPF Validation not working -
i created wpf serves serial number client. part of process, i'm trying validate length of each segment 5 characters long , of characters letters or numbers. followed outline given here.
the problem doesn't seem anything, , followed instructions submit button , submit incorrect data, whereas seems shouldn't validate if values aren't right. have binding path
? if not, why won't fields validate?
the code validationrule
looks this:
namespace syncagent.installer { class licensevalidationrule : validationrule { public override validationresult validate(object value, cultureinfo cultureinfo) { if(!(value.tostring().length == 5)) return new validationresult(false,"incorrect number of characters."); regex rexp = new regex("^[a-z0-9]*$"); if (!rexp.ismatch(value.tostring().toupper())) { return new validationresult(false,"a key may contain numbers , letters."); } return new validationresult(true, null); } } }
here's xaml:
<window x:name="wfusionkey" x:class="syncagent.installer.keyform" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:syncagent.installer" title="fusionkeyform" height="158" width="338" resizemode="noresize"> <grid margin="0,0,-6,1" horizontalalignment="left" width="334"> <grid.columndefinitions> <columndefinition width="0*"/> <columndefinition/> </grid.columndefinitions> <button x:name="btnokay" content="submit" horizontalalignment="left" margin="166,96,0,0" verticalalignment="top" width="75" grid.column="1" height="22" isdefault="true" click="btnokay_click"/> <label x:name="lblfusionlicensekey" content="please enter 25-digit fusion license key here." horizontalalignment="left" margin="10,10,0,0" verticalalignment="top" grid.column="1" height="26" width="278"/> <textbox x:name="txtfusion1" horizontalalignment="left" height="23" margin="10,57,0,0" textwrapping="wrap" verticalalignment="top" width="51" grid.column="1" maxlength="5" maxwidth="infinity"> <textbox.text> <binding path="/" mode="default" updatesourcetrigger="propertychanged"> <binding.validationrules> <local:licensevalidationrule/> </binding.validationrules> </binding> </textbox.text> </textbox> <button x:name="btncancel" grid.columnspan="2" content="cancel" horizontalalignment="left" margin="246,96,0,0" verticalalignment="top" width="75" iscancel="true"/> <textbox x:name="txtfusion2" grid.columnspan="2" horizontalalignment="left" height="23" margin="76,57,0,0" textwrapping="wrap" verticalalignment="top" width="50" maxlength="5"> <textbox.text> <binding path="left" updatesourcetrigger="propertychanged"> <binding.validationrules> <local:licensevalidationrule/> </binding.validationrules> </binding> </textbox.text> </textbox> <textbox x:name="txtfusion3" grid.columnspan="2" horizontalalignment="left" height="23" margin="141,57,0,0" textwrapping="wrap" verticalalignment="top" width="50" maxlength="5"> <textbox.text> <binding path="left" updatesourcetrigger="propertychanged"> <binding.validationrules> <local:licensevalidationrule/> </binding.validationrules> </binding> </textbox.text> </textbox> <textbox x:name="txtfusion4" grid.columnspan="2" horizontalalignment="left" height="23" margin="206,57,0,0" textwrapping="wrap" verticalalignment="top" width="50" maxlength="5" rendertransformorigin="1.491,0.522"> <textbox.text> <binding path="left" updatesourcetrigger="propertychanged"> <binding.validationrules> <local:licensevalidationrule/> </binding.validationrules> </binding> </textbox.text> </textbox> <textbox x:name="txtfusion5" grid.columnspan="2" horizontalalignment="left" height="23" margin="271,57,0,0" textwrapping="wrap" verticalalignment="top" width="50" maxlength="5"> <textbox.text> <binding path="left" updatesourcetrigger="propertychanged"> <binding.validationrules> <local:licensevalidationrule/> </binding.validationrules> </binding> </textbox.text> </textbox>
i'm not sure textbox
bound cannot see code behind, validator seems work ok when textbox
text bound string.
here simple example of validator attached textbox bound string.
code:
/// <summary> /// interaction logic mainwindow.xaml /// </summary> public partial class mainwindow : window, inotifypropertychanged { private string _mytexttovalidate; public mainwindow() { initializecomponent(); } public string mytexttovalidate { { return _mytexttovalidate; } set { _mytexttovalidate = value; notifypropertychanged("mytexttovalidate"); } } public event propertychangedeventhandler propertychanged; public void notifypropertychanged(string property) { if (propertychanged != null) { propertychanged(this, new propertychangedeventargs(property)); } } }
xaml:
<window x:class="wpfapplication11.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:wpfapplication11" title="mainwindow" height="89" width="166" name="ui"> <grid datacontext="{binding elementname=ui}"> <textbox height="23" margin="12" > <textbox.text> <binding path="mytexttovalidate" updatesourcetrigger="propertychanged"> <binding.validationrules> <local:licensevalidationrule/> </binding.validationrules> </binding> </textbox.text> </textbox> </grid> </window>
maybe track down issue.
Comments
Post a Comment