views:

237

answers:

3

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%'.

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

+1  A: 

You can use StringFormat like so

Content="{Binding Path=Value, ElementName=Slider, StringFormat='{0}%'}"
Chris
Thanks Chris. I've tried that (and many other configurations) with no luck. Any thoughts what might be happening?
Brad
That definately works as I have used it myself... What error do you get? What actually gets printed to the screen?
Chris
No error, it simply shows the digits. ie. '50' The label has no other properties set.
Brad
Actually...StringFormat='{0}%'doesn't work butStringFormat='{}{0}%'does? Strange, that!That is to say, it shows the digits only.
Brad
+1  A: 

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.

Dan Puzey
Thanks Dan, tried it in VS and no luck either.
Brad
I changed the label to a TextBlock and it works. I'll research why a label doesn't allow formatting. Thanks!
Brad
A: 

Here's the solution for WPF >=3.5 SP1:

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