StringFormat, ConverterCulture, and Decimal Text Field Label

I would like to solve the small quirk that I had. This is not a real quirk, but rather a behavior that I would like to change, if possible.

If I use {N: 2} StringFormat / ConverterCulture, the TextBox is forced to have a decimal mark always (even during text input). I mean, you cannot delete a dot or comma at all, you should be able to figure out that you need to go to the next β€œfield” of the number in order to edit the decimal points by clicking there or clicking β€œright”.

Since this non-initialist is for most users, is there a way to avoid this without resorting to rewriting formatters? I hope for something simple within the existing properties.

Example: a XAML text block is bound to a DataGrid 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 comments after replies:

  • UpdateSourceTrigger = PropertyChanged seems to be directly related to this behavior.
  • I suspect that a true complete solution may not be possible due to a logical conflict requiring both a true bi-directional update and a user trying to intervene with a new input at the same time.
0
c # wpf xaml string-formatting textbox
Jun 04 '15 at 16:14
source share
1 answer

The way the XAML event handler is installed explains that the TextBox1 control behavior: UpdateSourceTrigger=PropertyChanged sets the default behavior, which means that the source control ( TextBox1 ) is updated when the binding property changes. You can consider other TextBox events such as 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 simplified compact lambda-style syntax:

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

The same could be declared in XAML, but I recommend implementing functionality in the code module.

Regarding your second question, namely: CultureInfo implementation: you can save the CultureInfo declaration in XAML or implement it in the code module by placing it in any of the above event handlers, for example (re: Changing the default and decimal separator in the binding of Andrey Gordeev ):

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

Hope this helps.

+1
Jun 04 '15 at 17:23
source share



All Articles