StringFormat, ConverterCulture, and a textbox's decimal mark

痞子三分冷 提交于 2019-12-25 13:14:23

问题


I'd like to resolve a small quirk I've been having. It's not really a quirk but rather a behavior I'd like to change if possible.

If I use a {N:2} StringFormat/ConverterCulture, a TextBox is forced into having a decimal mark always (even during the process of inputting text). I mean, you can't delete the dot or the comma at all, you must be able to figure out you have to move to the next "field" of the number in order to edit the decimal points by either clicking with the mouse there or pressing "right".

Since that's uninituitive for most users, is there a way to avoid it without resorting to rewriting the formatters? I hope for something simple in the framework of the existing properties.

Example, a XAML TextBox bound to a 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 to be directly related to this behavior.
  • I suspect that a true full solution might not be possible due to a logical conflict of wanting both to have true bidirectional updating and a user trying to intervene with new input at the same time.

回答1:


The way you set the event handler in XAML explains that TextBox1 control behavior: UpdateSourceTrigger=PropertyChanged sets the default behavior, which means that the source control (TextBox1) is updated on that binding property change. You may consider other TextBox events, like LostFocus and TextChanged as 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)
{
// Your event handling procedure, Formatting, etc.
}

private void TextBox_TextChanged(object sender, RoutedEventArgs e)
{
// Your event handling procedure, Formatting, etc.
}

or using a Lambda-style simplified compact syntax:

TextBox1.LostFocus += (s, e) => {//Your procedure, Formatting, etc};
TextBox1.TextChanged += (s, e) => {//Your procedure, Formatting, etc};

The same could be also declared in XAML, but I recommend to implement the functionality in code-behind module.

In regards to your second question, namely CultureInfo implementation: you can keep the CultureInfo declaration in XAML, or implement it in code-behind module placing it in any of the aforementioned event's handler, e.g (re: Changing the default thousand and decimal separator in a binding by Andrey Gordeev):

String.Format(new CultureInfo("de-DE"), "{0:N}", valueTypeDouble);

Hope this may help.



来源:https://stackoverflow.com/questions/30649007/stringformat-converterculture-and-a-textboxs-decimal-mark

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!