c# - TextBoxInputMaskBehavior covers the value -
i have textboxinputmaskbehavior ip address looks follow:
, xaml code:
<usercontrol x:class="customizing.views.iprangefields" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:ig="http://schemas.infragistics.com/xaml" xmlns:local="clr-namespace:customizing.views" xmlns:net="clr-namespace:system.net;assembly=system" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:behaviors="clr-namespace:customizing.behaviors" xmlns:system="clr-namespace:system;assembly=mscorlib" mc:ignorable="d" x:name="uc" d:designheight="300" d:designwidth="300"> <usercontrol.resources> <system:string x:key="inputmaskip">000.000.000.000</system:string> </usercontrol.resources> <grid> <grid.rowdefinitions> <rowdefinition height="60" /> <rowdefinition height="10" /> <rowdefinition height="60" /> <rowdefinition height="10" /> <rowdefinition height="60" /> <rowdefinition height="10" /> <rowdefinition height="60" /> <rowdefinition height="10" /> </grid.rowdefinitions> <grid.columndefinitions> <columndefinition width="*" /> <columndefinition width="2.5*" /> </grid.columndefinitions> <label grid.row="0" grid.column="0" content="start" verticalcontentalignment="center" fontsize="18" horizontalcontentalignment="center" fontweight="bold" /> <label grid.row="2" grid.column="0" content="end" verticalcontentalignment="center" fontsize="18" horizontalcontentalignment="center" fontweight="bold" /> <label grid.row="4" grid.column="0" content="subnet" verticalcontentalignment="center" fontsize="18" horizontalcontentalignment="center" fontweight="bold" /> <label grid.row="6" grid.column="0" content="gateway" verticalcontentalignment="center" fontsize="18" horizontalcontentalignment="center" fontweight="bold" /> <textbox verticalcontentalignment="center" horizontalcontentalignment="center" grid.row="0" grid.column="1" fontsize="22" validation.error="_validationerror"> <textbox.text> <binding elementname="uc" path="start" validatesonnotifydataerrors="true" updatesourcetrigger="propertychanged" notifyonvalidationerror="true"> <binding.validationrules> <local:ipaddressrule /> </binding.validationrules> </binding> </textbox.text> <i:interaction.behaviors> <behaviors:textboxinputmaskbehavior inputmask="{staticresource inputmaskip}" promptchar="0" /> </i:interaction.behaviors> </textbox> <textbox verticalcontentalignment="center" horizontalcontentalignment="center" grid.row="2" grid.column="1" fontsize="22" validation.error="_validationerror"> <binding elementname="uc" path="end" validatesonnotifydataerrors="true" updatesourcetrigger="propertychanged" notifyonvalidationerror="true"> <binding.validationrules> <local:ipaddressrule /> </binding.validationrules> </binding> <i:interaction.behaviors> <behaviors:textboxinputmaskbehavior inputmask="{staticresource inputmaskip}" promptchar="0" /> </i:interaction.behaviors> </textbox> <textbox verticalcontentalignment="center" horizontalcontentalignment="center" grid.row="4" grid.column="1" fontsize="22" validation.error="_validationerror"> <textbox.text> <binding elementname="uc" path="subnet" validatesonnotifydataerrors="true" updatesourcetrigger="propertychanged" notifyonvalidationerror="true"> <binding.validationrules> <local:ipaddressrule /> </binding.validationrules> </binding> </textbox.text> <i:interaction.behaviors> <behaviors:textboxinputmaskbehavior inputmask="{staticresource inputmaskip}" promptchar="0" /> </i:interaction.behaviors> </textbox> <textbox verticalcontentalignment="center" horizontalcontentalignment="center" grid.row="6" grid.column="1" fontsize="22" validation.error="_validationerror"> <textbox.text> <binding elementname="uc" path="gateway" validatesonnotifydataerrors="true" updatesourcetrigger="propertychanged" notifyonvalidationerror="true"> <binding.validationrules> <local:ipaddressrule /> </binding.validationrules> </binding> </textbox.text> <i:interaction.behaviors> <behaviors:textboxinputmaskbehavior inputmask="{staticresource inputmaskip}" promptchar="0" /> </i:interaction.behaviors> </textbox> </grid> </usercontrol>
and code behind:
using system.globalization; using system.net; using system.windows; using system.windows.controls; namespace customizing.views { /// <summary> /// interaction logic iprangefields.xaml /// </summary> public partial class iprangefields : usercontrol { public static readonly dependencyproperty startproperty = dependencyproperty.register("start", typeof (string), typeof (iprangefields), new propertymetadata(null)); public static readonly dependencyproperty endproperty = dependencyproperty.register("end", typeof (string), typeof (iprangefields), new propertymetadata(null)); public static readonly dependencyproperty subnetproperty = dependencyproperty.register("subnet", typeof (string), typeof (iprangefields), new propertymetadata(null)); public static readonly dependencyproperty gatewayproperty = dependencyproperty.register("gateway", typeof (string), typeof (iprangefields), new propertymetadata(null)); // register routed event public static readonly routedevent errorevent = eventmanager.registerroutedevent("error", routingstrategy.bubble, typeof (routedeventhandler), typeof (iprangefields)); public iprangefields() { initializecomponent(); } public string start { { return (string) getvalue(startproperty); } set { setvalue(startproperty, value); } } public string end { { return (string) getvalue(endproperty); } set { setvalue(endproperty, value); } } public string subnet { { return (string) getvalue(subnetproperty); } set { setvalue(subnetproperty, value); } } public string gateway { { return (string) getvalue(gatewayproperty); } set { setvalue(gatewayproperty, value); } } public event routedeventhandler error { add { addhandler(errorevent, value); } remove { removehandler(errorevent, value); } } private void _validationerror(object sender, validationerroreventargs e) { raiseevent(new routedeventargs(errorevent, sender)); } } public class ipaddressrule : validationrule { public override validationresult validate(object value, cultureinfo cultureinfo) { ipaddress ip; if (!ipaddress.tryparse(value.tostring(), out ip)) { return new validationresult(false, "ip address not valid."); } return new validationresult(true, null); } } }
my problem can see on picture, subnet field shows value other 3 fields not. sure,
<behaviors:textboxinputmaskbehavior inputmask="{staticresource inputmaskip}" promptchar="0" />
covers values on 3 fields , hence can not see values.
make me confuse is, why value on subnet field appears? doing wrong other 3 fields?
textboxmask class:
using system.componentmodel; using system.globalization; using system.windows; using system.windows.controls; using system.windows.input; using system.windows.interactivity; namespace customizing.behaviors { /// <summary> /// inputmask textbox 2 properties: <see cref="inputmask" />, <see cref="promptchar" />. /// </summary> public class textboxinputmaskbehavior : behavior<textbox> { public maskedtextprovider provider { get; private set; } protected override void onattached() { base.onattached(); associatedobject.loaded += associatedobjectloaded; associatedobject.previewtextinput += associatedobjectpreviewtextinput; associatedobject.previewkeydown += associatedobjectpreviewkeydown; dataobject.addpastinghandler(associatedobject, pasting); } protected override void ondetaching() { base.ondetaching(); associatedobject.loaded -= associatedobjectloaded; associatedobject.previewtextinput -= associatedobjectpreviewtextinput; associatedobject.previewkeydown -= associatedobjectpreviewkeydown; dataobject.removepastinghandler(associatedobject, pasting); } /* mask character accepts required? 0 digit (0-9) required 9 digit (0-9) or space optional # digit (0-9) or space required l letter (a-z, a-z) required ? letter (a-z, a-z) optional & character required c character optional alphanumeric (0-9, a-z, a-z) required alphanumeric (0-9, a-z, a-z) optional space separator required . decimal separator required , group (thousands) separator required : time separator required / date separator required $ currency symbol required in addition, following characters have special meaning: mask character meaning < subsequent characters converted lower case > subsequent characters converted upper case | terminates previous < or > \ escape: treat next character in mask literal text rather mask symbol */ private void associatedobjectloaded(object sender, routedeventargs e) { provider = new maskedtextprovider(inputmask, cultureinfo.currentculture); provider.set(associatedobject.text); provider.promptchar = promptchar; associatedobject.text = provider.todisplaystring(); //seems way text formatted correct, when source updated var textprop = dependencypropertydescriptor.fromproperty(textbox.textproperty, typeof (textbox)); if (textprop != null) { textprop.addvaluechanged(associatedobject, (s, args) => updatetext()); } } private void associatedobjectpreviewtextinput(object sender, textcompositioneventargs e) { treatselectedtext(); var position = getnextcharacterposition(associatedobject.selectionstart); if (keyboard.iskeytoggled(key.insert)) { if (provider.replace(e.text, position)) position++; } else { if (provider.insertat(e.text, position)) position++; } position = getnextcharacterposition(position); refreshtext(position); e.handled = true; } private void associatedobjectpreviewkeydown(object sender, keyeventargs e) { if (e.key == key.space) //handle space { treatselectedtext(); var position = getnextcharacterposition(associatedobject.selectionstart); if (provider.insertat(" ", position)) refreshtext(position); e.handled = true; } if (e.key == key.back) //handle space { if (treatselectedtext()) { refreshtext(associatedobject.selectionstart); } else { if (associatedobject.selectionstart != 0) { if (provider.removeat(associatedobject.selectionstart - 1)) refreshtext(associatedobject.selectionstart - 1); } } e.handled = true; } if (e.key == key.delete) //handle delete key { //treat selected text if (treatselectedtext()) { refreshtext(associatedobject.selectionstart); } else { if (provider.removeat(associatedobject.selectionstart)) refreshtext(associatedobject.selectionstart); } e.handled = true; } } /// <summary> /// pasting prüft ob korrekte daten reingepastet werden /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void pasting(object sender, dataobjectpastingeventargs e) { if (e.dataobject.getdatapresent(typeof (string))) { var pastedtext = (string) e.dataobject.getdata(typeof (string)); treatselectedtext(); var position = getnextcharacterposition(associatedobject.selectionstart); if (provider.insertat(pastedtext, position)) { refreshtext(position); } } e.cancelcommand(); } private void updatetext() { //check provider.text + textbox.text if (provider.todisplaystring().equals(associatedobject.text)) return; //use provider format var success = provider.set(associatedobject.text); //ui , mvvm/codebehind should in sync settext(success ? provider.todisplaystring() : associatedobject.text); } /// <summary> /// falls eine textauswahl vorliegt wird diese entsprechend behandelt. /// </summary> /// <returns>true textauswahl behandelt wurde, ansonsten falls </returns> private bool treatselectedtext() { if (associatedobject.selectionlength > 0) { return provider.removeat(associatedobject.selectionstart, associatedobject.selectionstart + associatedobject.selectionlength - 1); } return false; } private void refreshtext(int position) { settext(provider.todisplaystring()); associatedobject.selectionstart = position; } private void settext(string text) { associatedobject.text = string.isnullorwhitespace(text) ? string.empty : text; } private int getnextcharacterposition(int startposition) { var position = provider.findeditpositionfrom(startposition, true); if (position == -1) return startposition; return position; } #region dependencyproperties public static readonly dependencyproperty inputmaskproperty = dependencyproperty.register("inputmask", typeof (string), typeof (textboxinputmaskbehavior), null); public string inputmask { { return (string) getvalue(inputmaskproperty); } set { setvalue(inputmaskproperty, value); } } public static readonly dependencyproperty promptcharproperty = dependencyproperty.register("promptchar", typeof (char), typeof (textboxinputmaskbehavior), new propertymetadata('_')); public char promptchar { { return (char) getvalue(promptcharproperty); } set { setvalue(promptcharproperty, value); } } #endregion } }
i copied code , runs without errors.
set values (iprange control):
iprange.start = "123.456.789.000"; iprange.end = "123.456.789.000"; iprange.subnet = "123.456.789.000"; iprange.gateway = "123.456.789.000";
result:
i think, problem binding or else, not in control.
one comment. change mask 000.000.000.000
000\.000\.000\.000
because .
decimal separator (in country comma, not dot, therefore can take troubles culture support).
Comments
Post a Comment