When you format a TextBox as currency and click to edit it, the $ and commas do not cause a problem, you can just edit and tab with no problem. When you format a field as a percentage things do not work so well.
<TextBox Text="{Binding CostMarkup,
StringFormat=P}"
Style="{StaticResource ctrlSpacingTight}" />
If the underlying value is 0.1 it correctly displays as 10%, if you go to edit it still shows as 10% the % will cause a problem plus it will change the underlying value from .1 to 10. I wrote a Converter to handle all this but I’m wondering if there isn’t a better way. In particular is there a way to handle it the way currency handles it?
There is a built in currency converter so I suspect the currency version of StringFormat uses that. While there is a ZoomPerentageConverter it doesn’t do what I would expect. Is there a way to hook in to StringFormat=P and have it invoke my Converter instead of having to go to every instance and explicitly specify it?
<TextBox Text="{Binding CostMarkup,
StringFormat=P,
Converter={StaticResource pctConverter}}"
Style="{StaticResource ctrlSpacingTight}" />
I’ve always found that it’s easiest to display the raw data when editing, and the formatted value when not.
Here’s an example that does that using a trigger
If
ctrlSpacingTightis a global style, you can create a style for your TextBox that isBasedOnyour global style.