27

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?

1
  • I cannot understand how did this xaml compile at all Commented Dec 21, 2010 at 11:24

7 Answers 7

77

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

Sign up to request clarification or add additional context in comments.

2 Comments

Why do you have open & close curly brackets listed first in your ContentStringFormat?
@ClearCloud8 Open & close curly braces are necessary in Binding...,StringFormat={}{0} when you need the {0} to be the first character. They are not necessary for ContentStringFormat, but they may be there, too.
19

Have you tried:

<ToolTip> <TextBlock Text="{Binding Path=DataItem.EquitySold, StringFormat=Reserved (Equity Share: \{0\}%)}" /> </ToolTip> 

2 Comments

Why couldn't he use the Value Property?
@TomerW: Mitch explains why in his answer.
2

For anyone else that winds up here in a slightly different situation this was desired for setting a tooltip StringFormat via Style:

<DataGridTextColumn Header="Amount" CanUserSort="True" Binding="{Binding Amount,Mode=OneWay}"> <DataGridTextColumn.CellStyle> <Style TargetType="DataGridCell"> <Setter Property="ToolTip"> <Setter.Value> <TextBlock Text="{Binding JournalEntryId, StringFormat='Reserved (Equity Share: \{0\}%)'}" /> </Setter.Value> </Setter> </Style> </DataGridTextColumn.CellStyle> </DataGridTextColumn> 

Comments

1

You don't need to escape the brackets. Try this (i like to put the format in single quotes):

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

1 Comment

I'm afraid that this didn't make any difference
1
<Button.ToolTip> <TextBlock Text="{Binding Path=ToggleText, StringFormat={}{0} Text}"/> </Button.ToolTip> 

Button inside DataGridTemplateColumn

Comments

0

I assume it is what your datatype supports - as far as I know it is passed on as arguments to IFormattable.

Comments

-1

Try

StringFormat=Reserved (Equity Share: {0:P0}) 

1 Comment

I'm afraid that this didn't make any difference.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.