WPF - Trouble binding ToolTip text on custom user control

I'm in the process of creating a simple user control; just an ImageButton.

I've already successfully bound the Image to the button and so I've decided to add a tooltip. Now I'm having troubles. It seems that I can hard-code the text for the tooltip in the XAML for the control, but when it's bound it's returning an empty string.

Here's the XAML for my control:

<Button x:Class="BCOCB.DACMS.Controls.ImageButton"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
        Name="this"
        Style="{StaticResource DisabledButton}">

    <Image Source="{Binding ElementName=this, Path=Source}" />    
    <Button.ToolTip>
        <TextBlock Text="{Binding ElementName=this, Path=ToolTipText}" />
    </Button.ToolTip>
</Button>

And here's the dependency property info for the tooltip text:

public static readonly DependencyProperty ToolTipTextProperty = DependencyProperty.Register("ToolTipText", typeof(string), typeof(ImageButton));
public string ToolTipText
{
  get
  {
    return this.GetValue(ToolTipTextProperty) as string;
  }
  set
  {
    this.SetValue(ToolTipTextProperty, value);
  }
}

And, finally, the declaration of the control in my Window:

<controls:ImageButton x:Name="btnAdd" Source="/DACMS;component/Resources/plus.png" ToolTipText="Add New Item" Click="btnAdd_Click" />

As I mentioned before, the image binds just fine and I've done it in exactly the same manner.

Any ideas?

Thanks, Sonny

EDIT: I have it working now. I've removed the ElementName from the binding and set the TextBlock's DataContext = this in the code behind on instanciation. Still, I'd like to know how to fix this in the XAML, instead.

Answers


I'm unable to test this right now, but you can try:

<Button.ToolTip
      DataContext=”{Binding Path=PlacementTarget.Parent.Parent,
                    RelativeSource={x:Static RelativeSource.Self}}"
>
    <TextBlock Text="{Binding Path=ToolTipText}" />
</Button.ToolTip>

You may have to experiment a little with the number of "Parent" in PlacementTarget.

Hopefully this works. I don't like giving answers that I haven't tested, but I don't have VS on this computer. :)


Need Your Help

How to resolve local DTDs without System.Xml.Utils.Dll(XmlPreloadedResolver)?

.net xml gac dtd resolve

I use an external DTD to validate my xml files. Since the XmlReader seem to look for the DTD only in the folder where the xml file is, I need to somehow tell the reader to look into a specific folder

How to create array-like data-structures with object keys in PHP?

php data-structures map

I want create arrays with object keys in PHP, i.e. something like this:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.