Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use StringFormat to round a bound double value

I cannot get my DataBinding to cut off the decimal digits of my sliders double value and add a % sign. I want that my view only displays "89%" even if the sliders value is 89.1234. Is it possible to use stringFormat in WPF DataBinding to realize that behavior?

<Label Content="{Binding ElementName=Slider, Path=Value}"/>

Thank you very much in advance for any help.

like image 581
Thomas Huber Avatar asked Feb 22 '12 14:02

Thomas Huber


1 Answers

You cannot use the Binding's StringFormat if you are also using a Label because the Label has a property called ContentStringFormat that overrides the binding's StringFormat

Either use the Label's ContentStringFormat property

<Label Content="{Binding ElementName=Slider, Path=Value}" 
       ContentStringFormat="{}{0:N0}%" />

Or switch to using a TextBlock

    <TextBlock Text="{Binding ElementName=Slider, Path=Value, StringFormat={}{0:N0}%}" />
like image 108
Rachel Avatar answered Nov 19 '22 14:11

Rachel