Click here to Skip to main content
15,868,049 members
Articles / Desktop Programming / WPF

.NET 3.5 SP1 StringFormat Binding Property Weirdness (A Rant)

Rate me:
Please Sign up or sign in to vote.
3.67/5 (2 votes)
17 Jun 2009CPOL2 min read 19.2K   3   2
This is a rant about .NET 3.5 SP1 StringFormat Binding Property

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 TextBoxes with custom Bindings.

  1. 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.
  2. 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.
  3. 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.

XML
 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.

37328/works-thumb.jpg

But this little blighter, just will not have it. Grrrr!

37328/dontwork-thumb.jpg

I am assuming this is some issue with WPF that will get resolved within a further release.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.

Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Comments and Discussions

 
GeneralToolTip binding Pin
crocbert23-Jun-09 11:23
crocbert23-Jun-09 11:23 
GeneralRe: ToolTip binding Pin
Sacha Barber23-Jun-09 21:39
Sacha Barber23-Jun-09 21:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.