[ACCEPTED]-WPF binding with StringFormat doesn't work on ToolTips-binding
ToolTips in WPF can contain anything, not 8 just text, so they provide a ContentStringFormat 7 property for the times you just want text. You'll 6 need to use the expanded syntax as far as 5 I know:
<TextBox ...>
<TextBox.ToolTip>
<ToolTip
Content="{Binding ElementName=myTextBlock,Path=Text}"
ContentStringFormat="{}It is: {0}"
/>
</TextBox.ToolTip>
</TextBox>
I'm not 100% sure about the validity 4 of binding using the ElementName syntax 3 from a nested property like that, but the 2 ContentStringFormat property is what you're 1 looking for.
It could be a bug. When you use short syntax 2 for tooltip:
<TextBox ToolTip="{Binding WhatEverYouWant StringFormat='It is: \{0\}'}" />
StringFormat is ignore but when 1 you use expanded syntax:
<TextBox Text="text">
<TextBox.ToolTip>
<TextBlock Text="{Binding WhatEverYouWant StringFormat='It is: \{0\}'}"/>
</TextBox.ToolTip>
</TextBox>
It works as expected.
As Matt said ToolTip can contain anything 4 inside so for your you could bind a TextBox.Text 3 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 Stack a 2 grid inside the ToolTip and layout your 1 text if you want.
Your code can be as short as this:
<TextBlock ToolTip="{Binding PrideLands.YearsTillSimbaReturns,
Converter={StaticResource convStringFormat},
ConverterParameter='Rejoice! Just {0} years left!'}" Text="Hakuna Matata"/>
We'll 3 use the fact Converters are never ignored, unlike 2 StringFormat.
Put this into 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 into 1 your ResourceDictionary.xaml:
<conv:StringFormatConverter x:Key="convStringFormat"/>
In this situation, you can use relative 1 binding:
<StackPanel>
<TextBlock x:Name="MyTextBlock">Foo Bar</TextBlock>
<TextBox Text="{Binding ElementName=MyTextBlock, Path=Text, StringFormat='It is: \{0\}'}"
ToolTip="{Binding Text, RelativeSource={RelativeSource Self}}" />
</StackPanel>
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.