Format Percentage
-
I have a nullable decimal TaxRate property on an entity. I want to format/style the textbox to enter a percentage. What I have so far doesn't work. If I start out by entering a decimal point, then it works fine. If I start out by entering a digit, then I cannot enter a decimal.
<Setter Property="Text" Value="{Binding SelectedStatesCountiesCities.TaxRate, StringFormat={}{0:N2}%}" /> <Style.Triggers> <Trigger Property="IsKeyboardFocused" Value="True"> <Setter Property="Text" Value="{Binding SelectedStatesCountiesCities.TaxRate}" /> </Trigger> </Style.Triggers>
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
-
I have a nullable decimal TaxRate property on an entity. I want to format/style the textbox to enter a percentage. What I have so far doesn't work. If I start out by entering a decimal point, then it works fine. If I start out by entering a digit, then I cannot enter a decimal.
<Setter Property="Text" Value="{Binding SelectedStatesCountiesCities.TaxRate, StringFormat={}{0:N2}%}" /> <Style.Triggers> <Trigger Property="IsKeyboardFocused" Value="True"> <Setter Property="Text" Value="{Binding SelectedStatesCountiesCities.TaxRate}" /> </Trigger> </Style.Triggers>
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
The
UpdateSourceTrigger=PropertyChanged
option means that your source property is updated every time you press a key in theTextBox
. When you type a . character, one of two things will happen:- The box is currently empty.
"."
on its own cannot be parsed as adecimal
, so the property will not be updated. - The box contains an integer:
decimal.Parse("42.")
returns42
.- The property is updated to
42
, triggering thePropertyChanged
event. - The binding on the box updates to reflect the new property value - the text is set to
"42"
.
Depending on what you are trying to do, there may be ways around this - for example, manually updating the source value when the user presses Enter: c# - Binding to a double with StringFormat on a TextBox - Stack Overflow[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
- The box is currently empty.
-
The
UpdateSourceTrigger=PropertyChanged
option means that your source property is updated every time you press a key in theTextBox
. When you type a . character, one of two things will happen:- The box is currently empty.
"."
on its own cannot be parsed as adecimal
, so the property will not be updated. - The box contains an integer:
decimal.Parse("42.")
returns42
.- The property is updated to
42
, triggering thePropertyChanged
event. - The binding on the box updates to reflect the new property value - the text is set to
"42"
.
Depending on what you are trying to do, there may be ways around this - for example, manually updating the source value when the user presses Enter: c# - Binding to a double with StringFormat on a TextBox - Stack Overflow[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Thanks for the reply. Setting UpdateSourceTrigger to LostFocus dit it BTW, here's an interesting solution: [c# - How do I get a TextBox to only accept numeric input in WPF? - Stack Overflow](https://stackoverflow.com/questions/1268552/how-do-i-get-a-textbox-to-only-accept-numeric-input-in-wpf)
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
- The box is currently empty.