c# - Attached Property on TextBox WPF -
my problem simple have textbox in wpf can type number. want 3 things when press specific keys :
- enter : value sent
- up : value increased sent
- down : value decreased sent
i have made 3 attached properties works key enter.
<textbox margin="5,0" hlp:textboxbehaviors.keytovalid="enter" hlp:decrease.keytodecrease="down" hlp:increase.keytoincrease="up" text="{binding target, mode=twoway, updatesourcetrigger=explicit, converter={staticresource kiloconverter}, validatesonnotifydataerrors=true, validatesonexceptions=true}" /> the code of class :
public static class decrease { private static key triggeredkey = key.down; public static key getkeytodecrease(dependencyobject obj) { return (key)obj.getvalue(keytodecreaseproperty); } /// <summary> /// sert uniquement à s'attacher à la textbox (une seule fois à l'init de l'interface) /// </summary> public static void setkeytodecrease(dependencyobject obj, key value) { var tb = obj textbox; if (tb != null) tb.keydown += onkeydown; } /// <summary> /// evenement classique /// </summary> private static void onkeydown(object sender, keyeventargs e) { var tbcurrent = sender textbox; int selectedpos; if (tbcurrent != null && e.key == triggeredkey) { if (tbcurrent.selectionstart > 0) { if (!tbcurrent.text.contains(".")) { int exp = tbcurrent.text.length - tbcurrent.selectionstart; tbcurrent.text = (convert.todouble(tbcurrent.text) - math.pow(10, exp)).tostring(); selectedpos = tbcurrent.text.length - exp; } else { int exp; if (tbcurrent.selectionstart > tbcurrent.text.indexof('.') + 1) { exp = tbcurrent.text.indexof('.') - tbcurrent.selectionstart + 1; } else if (tbcurrent.selectionstart < tbcurrent.text.indexof('.') + 1) { exp = tbcurrent.text.indexof('.') - tbcurrent.selectionstart; } else { return; } tbcurrent.text = (convert.todouble(tbcurrent.text) - math.pow(10, exp)).tostring("0.00"); if (exp < 0) { selectedpos = tbcurrent.text.indexof('.') - exp + 1; } else { if (tbcurrent.text.indexof('.') - exp < 0) { selectedpos = 0; } else { selectedpos = tbcurrent.text.indexof('.') - exp; } } } bindingexpression binding = tbcurrent.getbindingexpression(textbox.textproperty); if (binding != null) { binding.updatesource(); } tbcurrent.selectionlength = 0; if (selectedpos >= 0) { tbcurrent.selectionstart = selectedpos; } else { tbcurrent.selectionstart = 0; } } } } // using dependencyproperty backing store keytovalid. enables animation, styling, binding, etc... public static readonly dependencyproperty keytodecreaseproperty = dependencyproperty.registerattached("keytodecrease", typeof(key), typeof(textbox), new propertymetadata(triggeredkey)); } this code decrease attached behavior 2 others designed in same way.
the decrease , increase don't trigger event ! enter does...
the decrease , increase don't trigger event ! enter does...
handle previewkeydown event:
public static void setkeytodecrease(dependencyobject obj, key value) { var tb = obj textbox; if (tb != null) tb.previewkeydown += onkeydown; } the textbox control handles , "swallows" key presses on own. that's why keydown event isn't fired in behaviour.
Comments
Post a Comment