c# - StringFormat, ConverterCulture, and a textbox's decimal mark -
i'd resolve small quirk i've been having. it's not quirk rather behavior i'd change if possible.
if use {n:2} stringformat/converterculture, textbox forced having decimal mark always (even during process of inputting text). mean, can't delete dot or comma @ all, must able figure out have move next "field" of number in order edit decimal points either clicking mouse there or pressing "right".
since that's uninituitive users, there way avoid without resorting rewriting formatters? hope simple in framework of existing properties.
example, xaml textbox bound datagrid's cell,
<textbox name="textbox1" height="18" margin="0,0,10,0" text="{binding selecteditem[1], converterculture=en-us, elementname=grid1, stringformat={}{0:n2}, updatesourcetrigger=propertychanged}" width="59" textalignment="right" verticalalignment="center" />
added remarks after answers:
- updatesourcetrigger=propertychanged appears directly related behavior.
- i suspect true full solution might not possible due logical conflict of wanting both have true bidirectional updating , user trying intervene new input @ same time.
the way set event handler in xaml explains textbox1
control behavior: updatesourcetrigger=propertychanged
sets default behavior, means source control (textbox1
) updated on binding property change. may consider other textbox
events, lostfocus
, textchanged
shown below (c#):
textbox1.lostfocus += (s, e) => textbox_lostfocus(s, e); textbox1.textchanged += (s, e) => textbox_textchanged(s, e); private void textbox_lostfocus(object sender, routedeventargs e) { // event handling procedure, formatting, etc. } private void textbox_textchanged(object sender, routedeventargs e) { // event handling procedure, formatting, etc. }
or using lambda-style simplified compact syntax:
textbox1.lostfocus += (s, e) => {//your procedure, formatting, etc}; textbox1.textchanged += (s, e) => {//your procedure, formatting, etc};
the same declared in xaml, recommend implement functionality in code-behind module.
in regards second question, namely cultureinfo
implementation: can keep cultureinfo
declaration in xaml, or implement in code-behind module placing in of aforementioned event's handler, e.g (re: changing default thousand , decimal separator in binding andrey gordeev):
string.format(new cultureinfo("de-de"), "{0:n}", valuetypedouble);
hope may help.
Comments
Post a Comment