c# - WPF: Textbox + Slider with min values -
i have textbox binded slider, , slider has minimum value set.
the problem if start entering values in textbox out of min - automatically transferred min. e.g. if set min 4, , want type 12, once press 1, it's changed 4 in textbox, , can't enter 12, instead 42. if start typing 4 or 5 (say 42, or 51, etc.) ok.
is there way defer check of min until after user has pressed enter ?
here's xaml:
<textbox text="{binding elementname=maxvalue, path=value, updatesourcetrigger=propertychanged}" textalignment="center" verticalcontentalignment="center" width="30" height="25" borderbrush="transparent"></textbox> <slider value="{binding totalsize}" maximum="{binding maxmaxbackupsize}" minimum="{binding minbackupsize}" tickplacement="bottomright" tickfrequency="2" issnaptotickenabled="true" name="maxvalue"></slider>
set updatesourcetrigger
property lostfocus
, press tab:
<textbox text="{binding elementname=maxvalue, path=value, updatesourcetrigger=lostfocus}" textalignment="center" verticalcontentalignment="center" width="30" height="25" borderbrush="transparent"></textbox>
or press enter , handle previewkeydown
event this:
private void textbox_previewkeydown(object sender, keyeventargs e) { if (e.key == key.enter) { e.handled = true; textbox textbox = sender textbox; textbox.movefocus(new traversalrequest(focusnavigationdirection.next)); } }
or explicitly update source property suggested @clemens:
private void textbox_previewkeydown(object sender, keyeventargs e) { if (e.key == key.enter) { e.handled = true; textbox textbox = sender textbox; bindingexpression = textbox.getbindingexpression(textbox.textproperty); be.updatesource(); } }
Comments
Post a Comment