Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XAML: How do I append a percent symbol to a bound label value?

I have a label bound to the value of a slider.

Content="{Binding Path=Value, ElementName=Slider}"

How do I append a percentage symbol? The value of the slider is already formatted correctly, so when the value is '50', all I need is '50%'.

I know how to do it in code behind but I was hoping to accomplish this in xaml without creating a converter. TIA

like image 244
Brad Avatar asked Mar 16 '10 16:03

Brad


4 Answers

I had a similar issue and solved it by using this, based on @Wiesel's answer:

<Label Content="{Binding Value, ElementName=Slider}" 
       ContentStringFormat="{}{0}%"/>
like image 52
Igor Pashchuk Avatar answered Nov 12 '22 12:11

Igor Pashchuk


This works fine for me (tested in Kaxaml):

<StackPanel>  
  <Slider Minimum="0" Maximum="100" x:Name="slider" />
  <TextBlock Text="{Binding Path=Value, ElementName=slider, StringFormat='\{0\}%'}" />
</StackPanel>

Without the backslashes I got an error saying that the % character was invalid in that position.

like image 38
Dan Puzey Avatar answered Nov 12 '22 12:11

Dan Puzey


StringFormat can be used in this format as well

Content="{Binding Path=Value, ElementName=Slider, StringFormat=P2}"

like image 27
Satya Avatar answered Nov 12 '22 12:11

Satya


You can use StringFormat like so

Content="{Binding Path=Value, ElementName=Slider, StringFormat='{0}%'}"
like image 1
Chris Avatar answered Nov 12 '22 13:11

Chris