Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Binding - StringFormat - Not Formatting

Tags:

I have a ToolTip with a value set as:

Value="{Binding Path=DataItem.EquitySold, StringFormat=Reserved (Equity Share: \{0\}%)}" 

The toolip is displaying as:

72

But I expect it to be:

Reserved (Equity Share: 72%)

What is wrong with my binding?

like image 292
David Ward Avatar asked Dec 21 '10 11:12

David Ward


2 Answers

A toolTip is a content control, which means it doesn't really have a display model. This is demonstrated in the earlier answer by @deccyclone that sets the content to a TextBlock. Since the TextBox is designed to display text, the StringFormat binding property works as advertised. Button is another example of this. (Both derive from ContentControl)

If you set the Content of a ToolTip to a string, the string is displayed because the ToolTip has a built in converter if the dataType is string. If you want to take advantage of that built in string converter, you need to set the format using the ContentStringFormat property.

<ToolTip      Content="{Binding Path=Value}"      ContentStringFormat="{}{0:F2} M" /> 

BTW, the tip off for when to use StringFormat or ContentStringFormat is by which property the control supplies for setting the displayed text. Text property -> use StringFormat Content property -> use ContentStringFormat

like image 102
Mitch Avatar answered Sep 19 '22 15:09

Mitch


Have you tried:

<ToolTip>     <TextBlock Text="{Binding Path=DataItem.EquitySold, StringFormat=Reserved (Equity Share: \{0\}%)}" /> </ToolTip> 
like image 25
decyclone Avatar answered Sep 18 '22 15:09

decyclone