Reputation: 3699
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:
Upvotes: 0
Views: 2157
Reputation: 7918
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.
Upvotes: 1