0

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.
j riv
  • 3,593
  • 6
  • 39
  • 54
  • Side note: adding "I would like it to behave .... " to the post in addition to "`{N:2}`" does not behave like I want" would make post nicer. – Alexei Levenkov Jun 04 '15 at 16:19
  • Please include the code snippet highlighting the event handlers of that TextBox control causing the issue you have described. Best regards, – Alexander Bell Jun 04 '15 at 16:23
  • Added one. Though I hadn't because I considered it default behavior. Perhaps the fact that it's bound to a DataGrid's cell might be related, I haven't tested it without it (though I must use it that way). – j riv Jun 04 '15 at 16:32
  • A more general but related thread: https://stackoverflow.com/questions/11477821/how-can-i-format-a-decimal-bound-to-textbox-without-angering-my-users – j riv Jun 04 '15 at 17:55
  • As I hinted in a comment on the first (and currently only) answer, I suspect this might be something that can't be easily done if I want BOTH to update source and destination automatically and auto-format them at the same time. At least maybe not in a very straightforward way. The reason might be a logical dead end that is not a limitation of the language but a limitation of a "conflict" of wanting both autoformatting and 100% bidirectional updating based on new input. – j riv Jun 04 '15 at 19:52

1 Answers1

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.

Community
  • 1
  • 1
Alexander Bell
  • 7,842
  • 3
  • 26
  • 42
  • ConverterCulture is important to me (for non en-US values). Would that solution mean I would have to provide a custom way to do that conversion on top? Or is it only about the UpdateSourceTrigger in isolation? – j riv Jun 04 '15 at 17:33
  • 1
    You can implement it as shown in my extended answer. Best regards, – Alexander Bell Jun 04 '15 at 17:42
  • I'm now thinking that while this answer is great help in general and thanks for that, it doesn't solve this particular problem. Of course, it does give a pointer on where to start. Then again, one could say it doesn't prove that it would solve it for sure (I'm currently finding it hard to find a method that BOTH updates the source after typing and doesn't "piss-off-the-user" and I start suspecting it is either impossible, or, it might need a middle ground, e.g. "update the source before losing focus, but only in these rare occasions" etc.I think I'll use the method in any case at least partly. – j riv Jun 04 '15 at 19:48
  • I have provided a technical solution to your question(s) based on the original post. If you have more questions, then please post them separately and clarify the business logic that you want to implement in the code: for us it's hard to guess on what doesn't "piss-off-the-user" of your app (such sentiments are actually subjective and opinion-based). Best regards, – Alexander Bell Jun 04 '15 at 21:21
  • I don't think you get what I said.I appreciate your help but I suspect there is a logical conflict in that PARTICULAR problem, not your method, your method is fine. Namely, it seems that if you want to BOTH have a 100% bidirectional binding between a box and a source like a grid but at the same time have a user type in data, there might be a conflict of autoformatting and not leaving him be to do his job of typing freely.Therefore, there might be a reason 99% of programs out there appear to just make you unfocus to save/update a particular word,not before(including MSWord come to think of it). – j riv Jun 04 '15 at 21:25
  • 1
    Thanks. Wrapping up this comment thread: based on my guess, it seems like you are looking for so-called "Edit Mask" TextBox solution; such solution exists but the implementation requires good deal of coding (re: http://stackoverflow.com/questions/534326/wpf-edit-mask). Best regards, – Alexander Bell Jun 05 '15 at 00:38
  • I think I'll just use a good middle ground at the moment. I'll use a good established method for focusing the boxes and maybe clearing the fancy formatting before editing, and then just save/bind only when focus is lost, not every single char change (because that was the original purpose, to autosave changes on every char change). I still suspect that this thing can't be really done "fully" or gracefully (updating and typing at the same time without disrupting the intuitive intentions of the user) so, I think it's approximately the same to just make it clean and update info when focus is lost. – j riv Jun 05 '15 at 13:15