Binding WPF to StringFormat not working on tooltips

The following code has a simple binding that binds TextBlock text named MyTextBlock with TextBox Text and ToolTip using the same binding:

<StackPanel> <TextBlock x:Name="MyTextBlock">Foo Bar</TextBlock> <TextBox Text="{Binding ElementName=MyTextBlock, Path=Text, StringFormat='It is: \{0\}'}" ToolTip="{Binding ElementName=MyTextBlock, Path=Text, StringFormat='It is: \{0\}'}" /> </StackPanel> 

Binding also uses the StringFormat property introduced with .NET 3.5 SP1 , which works fine for the specified Text property, but seems to have a tooltip. Expected result: "This: Foo Bar", but when you hover over the TextBox, the ToolTip shows only the binding value, not the formatted string value. Any ideas?

+70
wpf binding
Oct 13 '08 at 9:31
source share
5 answers

The following is a verbose solution, but it works.

 <StackPanel> <TextBox Text="{Binding Path=., StringFormat='The answer is: {0}'}"> <TextBox.DataContext> <sys:Int32>42</sys:Int32> </TextBox.DataContext> <TextBox.ToolTip> <ToolTip Content="{Binding}" ContentStringFormat="{}The answer is: {0}" /> </TextBox.ToolTip> </TextBox> </StackPanel> 

I would prefer a much simpler syntax similar to the one in my original question.

-7
Oct 13 '08 at 12:18
source share

Tooltips in WPF can contain anything, not just text, so they provide the ContentStringFormat property for the time you just want the text. As far as I know, you need to use the advanced syntax:

 <TextBox ...> <TextBox.ToolTip> <ToolTip Content="{Binding ElementName=myTextBlock,Path=Text}" ContentStringFormat="{}It is: {0}" /> </TextBox.ToolTip> </TextBox> 

I'm not 100% sure about the reality of the binding using the ElementName syntax from a nested property like this, but the ContentStringFormat property is what you are looking for.

+130
Oct 13 '08 at 9:45
source share

It could be a mistake. When you use short syntax for tooltip:

 <TextBox ToolTip="{Binding WhatEverYouWant StringFormat='It is: \{0\}'}" /> 

StringFormat is ignored, but when you use the extended syntax:

 <TextBox Text="text"> <TextBox.ToolTip> <TextBlock Text="{Binding WhatEverYouWant StringFormat='It is: \{0\}'}"/> </TextBox.ToolTip> </TextBox> 

It works as expected.

+12
Jul 31 '14 at 9:54
source share

As Matt said, ToolTip may contain something inside, so for you you can bind TextBox.Text inside your ToolTip.

 <StackPanel> <TextBlock x:Name="MyTextBlock">Foo Bar</TextBlock> <TextBox Text="{Binding ElementName=MyTextBlock, Path=Text, StringFormat='It is: \{0\}'}"> <TextBox.ToolTip> <TextBlock> <TextBlock.Text> <Binding ElementName=MyTextBlock Path="Text" StringFormat="It is: {0}" /> </TextBlock.Text> </TextBlock> </TextBox.ToolTip> </TextBox> </StackPanel> 

Even you can set the grid inside the tooltip and place your text if you want.

+4
Jan 13 '14 at 16:31
source share

Your code may be shorter:

 <TextBlock ToolTip="{Binding PrideLands.YearsTillSimbaReturns, Converter={StaticResource convStringFormat}, ConverterParameter='Rejoice! Just {0} years left!'}" Text="Hakuna Matata"/> 

We will use the fact that converters are never ignored, unlike StringFormat.

Put this in StringFormatConverter.cs :

 using System; using System.Globalization; using System.Windows; using System.Windows.Data; namespace TLKiaWOL { [ValueConversion (typeof(object), typeof(string))] public class StringFormatConverter : IValueConverter { public object Convert (object value, Type targetType, object parameter, CultureInfo culture) { if (ReferenceEquals(value, DependencyProperty.UnsetValue)) return DependencyProperty.UnsetValue; return string.Format(culture, (string)parameter, value); } public object ConvertBack (object value, Type targetType, object parameter, CultureInfo culture) { throw new NotSupportedException(); } } } 

Put this in your ResourceDictionary.xaml :

 <conv:StringFormatConverter x:Key="convStringFormat"/> 
+2
Dec 25
source share



All Articles