How to put decimal value in textbox? Example, I want to put value 0.20 or 20 but to be read as 20%?
2 Answers
Because you should keep this kind of code out of your business logic, you might want to use a StringFormat combined with a custom value converter. Please refer to this article.
In your case, the TextBox might - without knowing your code - look like this:
<TextBox Text="{Binding PercentValue, StringFormat=P, Converter={StaticResource PercentageConverter}}" /> The "P" here wold treat 0.25 as 25%.
1 Comment
Karolis Kajenas
Converter has to be imported as a resource to the XAML. Your answer is incomplete and yet copied without understand of how it works yourself.
StringFormatin your binding, as shown in the linked question. If you need something more advanced such as converting either "0.2" or "20" to a percent, you can use aConverter. For an example of a converter that only converts "20", check here - you should be able to easily modify that to check for something like ".20" too.