Click here to Skip to main content
15,746,652 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have tried the following XAML code. The value entered in the TextBox will be displayed in the TextBlock, but will no conversion has taken place. What am I doing wrong??

<StackPanel Orientation="Horizontal">
    <TextBox Name="waarde" Width="100" />
    <TextBlock Text="{Binding Path=Text, ElementName=waarde, 
                     StringFormat= {}{0:C1}}"/>

What I have tried:

I have used different formats for the StringFormat, but the result is always the same. in different
Updated 10-Feb-22 22:33pm

1 solution

You can't apply a format string to a string input. You need to convert the text to a numeric type before formatting it.

If you're using MVVM, you can bind the TextBox text to a decimal? property on the view-model. Bind the TextBlock text to the same property, and the string format will just work.

Otherwise, you'll need to write a custom IValueConverter to convert the entered string to a decimal before formatting it.
Share this answer
Maciej Los 14-Feb-22 15:04pm    
Antoon Crama 14-Feb-22 20:16pm    
Thanks. This works.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900