Search code examples
c#wpftextblockstring-formatting

Hide zero values with StringFormat


I made a similar question months ago

In this case I have a textblock like this

<TextBlock FontSize="28" Text="{Binding DataPoint.Y, StringFormat=\{0:0\\%\}}" Foreground="Black">

As you can see, my StringFormat puts a '%' sign after the number, if my data is 0.0 (I fill the component in code behind, my variable is a double) I get "0%"

But now I want to get "" if my text is 0.0

So far I have this:

 Text="{Binding DataPoint.Y, StringFormat=\{0:#.#\\%\}}"

But this retrieves "%", how can I get ""?


Solution

  • This worked for me

    {0:#\\%;0:#;#}