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

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 1st 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 2nd 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 2nd 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 to say 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 2nd

Works

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

DontWork

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

13 Comments so far »

  1. Corrado Cavalli said

    am October 19 2008 @ 3:06 pm

    Hi Sacha,
    I agree with you that having StringFormati working for tooltips would be a great nice to have, while waiting for the next release you can use:

  2. Wade Beasley said

    am October 20 2008 @ 3:26 am

    Sacha,

    As with all of your tutorials you do a great job. I have an off-topic question for you.

    How would you approach doing a Silverlight 2 version of the world map showcase on http://www.silverlight.net/world ?

    I understand it was done with SL 1 and Javascript. Can you give me some direction or a small sample of how you would approach the same thing?

    Thanks,
    Wade Beasley

  3. Rudi Grobler said

    am October 20 2008 @ 7:43 am

    Hi Sacha,

    the problem is that the ToolTip is a ContentControl… Try out the ContentStringFormat?

    Will test on my side!

  4. sacha said

    am October 20 2008 @ 7:51 am

    Aha.

    Thanks Rudi, that sounds logical, Ill try that now.

  5. sacha said

    am October 20 2008 @ 7:59 am

    Wade

    I would literally start with trying to locate the javascript code and take it from there. Other than that this looks to be just standard WPF storyboard image type stuff. You should not struggle with it too much

  6. Corrado Cavalli said

    am October 20 2008 @ 4:08 pm

    My comment has been truncated…
    Here’s a possible solution:

  7. sacha said

    am October 20 2008 @ 7:10 pm

    Corrado has a simple solution. Which I did know was possible, my rant was more about the use of StringFormat. But here is corrados solution anyway

    <TextBox x:Name="txt3" Width="180" Height="25">
    
            <TextBox.ToolTip>
    
              <ToolTip ContentStringFormat="Element is {0} wide"
               Content="{Binding Path=PlacementTarget.Width,
               RelativeSource={RelativeSource Self}}"/>
    
            </TextBox.ToolTip>
    
          </TextBox>
    
  8. Rudi Grobler said

    am October 21 2008 @ 6:54 am

    Sacha,

    You can also explicitly place a TextBlock inside your ToolTip and then use the exact same binding…

    It sucks but it works!

  9. sacha said

    am October 21 2008 @ 8:35 am

    Thanks man, this was not the problem, I just wanted to air my concern that I thought the StringFormat would work with the ToolTip.

    No worries.

  10. Mike Strobel said

    am October 27 2008 @ 4:53 pm

    I think the problem is due to the fact that the TextBox’s ToolTip is not actually a part of the logical tree. If you declare the TextBox’s tooltip with a ToolTip element and not with a String property, and you bind the Content property to “{Binding ElementName=txt3, Path=ActualWidth, Mode=Default}”, you will get an empty tool tip. I presume this is because the ToolTip is not within the TextBox’s NameScope, and thus ‘txt3′ cannot be resolved. However, if you set the binding directly on the TextBox via the ToolTip attribute, you are technically specifying a value on the TextBox, which *is* a part of the logical tree. The ToolTip element is created by a value conversion (you gave it a string, not a ToolTip). I’m guessing this is where the StringFormat on the Binding gets ignored. I’m just not sure how or why, but if someone wants to dig in and investigate, I’d be interested to hear what they find.

  11. sacha said

    am October 27 2008 @ 5:24 pm

    Mike

    You could be right. I got fed up with actually and used Corrados approach instead. GRRRRR

  12. Matt said

    am June 9 2010 @ 5:00 am

    I’m going to guess the binding doesn’t work properly because the target property is of type object rather than string – I encountered the same issue on MenuItem.Header. Thanks for your post – it led me to MenuItem.HeaderStringFormat.

  13. sacha said

    am June 9 2010 @ 7:37 am

    Cool cool

Comment RSS · TrackBack URI

Leave a comment

Name: (Required)

eMail: (Required)

Website:

Comment: