There are some great things in the .NET 3.5 SP1 for sure, one of them is very useful which is the new StringFormat
Binding property, which means that you do not need to write ValueConverters
any more to create a formatted value for the Binding. I really like this little addition to WPF.
The only thing is that I had a need to use it the other day within a ToolTip and it plain did not want to know. Consider the following XAML where I am using 3 separate TextBox
es with custom Bindings.
- The first one uses the
StringFormat
Binding property to create a Binding on the TextBox.Text
property. This is all cool and works as expected.
- The second one uses a custom
ToolTip
for a TextBox
just to prove we can do it. This is all cool and works as expected.
- The second one uses a custom
ToolTip
for a TextBox
, using the same Binding we used in Number 1, but we instead use the ToolTip
property instead of Text
. And this doesn’t use the string
in the StringFormat
property at all. It does use some value, but only the raw value, it seems to abandon the rest of the Format String
. Very strange.
Now I have no answer to this apart from saying that you may have to resort to ValueConverters
if you want a formatted TooTip
based on a bound value. I just thought this little issue may be interesting to someone reading this post. Basically, it is a little rant that I decided to document.
Here is the code and screen shot to back up my points.
1: <Window x:Class="StringFormatBinding.Window1"
2: xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4: Title="StringFormat Binding Weirdness" Height="300" Width="300"
5: SizeToContent="WidthAndHeight"
6: WindowStartupLocation="CenterScreen">
7: <StackPanel Orientation="Vertical">
8:
9: <!– This binding using StringFormat works just fine–>
10: <StackPanel Orientation="Horizontal" Margin="5"
11: Background="WhiteSmoke">
12: <Label Content="This Custom Binding Works Fine"/>
13: <TextBox x:Name="txt1" Width="180" Height="25"
14: Text="{Binding Path=Width,
15: ElementName=txt1, Mode=Default,
16: StringFormat=’Element is {0} Wide’}"/>
17: </StackPanel>
18:
19: <!– Are here is a custom tooltip for a TextBox–>
20: <StackPanel Orientation="Horizontal" Margin="5"
21: Background="WhiteSmoke">
22: <Label Content="This Custom ToolTip is ok"/>
23: <TextBox x:Name="txt2" Width="180" Height="25"
24: ToolTip="CUSTOM TOOL TIP"/>
25: </StackPanel>
26:
27: <!– So why doesnt the binding using StringFormat
28: work for ToolTip –>
29: <StackPanel Orientation="Horizontal" Margin="5"
30: Background="WhiteSmoke">
31: <Label Content="This Custom ToolTip is ok"/>
32: <TextBox x:Name="txt3" Width="180" Height="25"
33: ToolTip="{Binding Path=Width,
34: ElementName=txt3, Mode=Default,
35: StringFormat=’Tooltip : Element is {0} Wide’}"/>
36: </StackPanel>
37:
38: </StackPanel>
39: </Window>
And here is a screen shot of what I am talking about.
The top TextBox
works, as does the second.
But this little blighter, just will not have it. Grrrr!
I am assuming this is some issue with WPF that will get resolved within a further release.