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

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

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.

Advertisements

14 thoughts on “.NET 3.5 SP1 StringFormat Binding Property Weirdness (A Rant)

  1. Corrado Cavalli says:

    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 says:

    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 says:

    Hi Sacha,

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

    Will test on my side!

  4. sacha says:

    Aha.

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

  5. sacha says:

    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 says:

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

  7. sacha says:

    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 says:

    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 Barber on .NET 3.5 SP1 StringFormat Binding Property Weirdness (A Rant) […]

  10. sacha says:

    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.

  11. Mike Strobel says:

    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.

  12. sacha says:

    Mike

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

  13. Matt says:

    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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: