WPF is the text associated with the error. Tooltip text in a user control.

I am creating a simple user control; just ImageButton.

I have already successfully attached the image to the button, and so I decided to add a tooltip. Now I have a problem. It seems that I can hardcode the text for the tooltip in XAML for the control, but when it bound it to return an empty string.

Here is 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 is the dependency property information 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 control declaration in my window:

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

As I mentioned earlier, the image is perfectly linked, and I did it exactly the same.

Any ideas?

Thanks,
Sonny

EDITOR: I am working now. I removed the ElementName from the binding and set TextBlock DataContext = this to the code behind the instanciation. However, I would like to know how to fix this in XAML.

+4
source share
5 answers

I can't check it 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 need to experiment a little with the amount of β€œParent” in the PlacementTarget.

Hope this works. I do not like to give answers that I have not tested, but on this computer I do not have VS. :)

+6
source

I had the same problem with binding to ContextMenu. After my research, I think this is because ToolTip and ContextMenu do not exist in the visual tree of your page / window / control. And so the DataContext is not inherited and makes binding difficult.

Here is the Xaml hack I found that worked for me.

binding to a MenuItem in the WPF context menu

+1
source

The way to set the data context to "this" via xaml is as follows:

 <Control DataContext={Binding RelativeSource={RelativeSource Self}}> 

As another point, the wpf buttons allow their content to be just about any (single) thing you want. If you want something different from the text (i.e. Text and image), it looks like this:

 <Button Name="SampleButton" Click="SampleButton_Click"> <Grid Width="70" Height="62"> <Label Content="SampleText"/> <Image Margin="3,3,3,3" Source="Graphics/sample.ico"/> </Grid> </Button> 
+1
source

Since you do not change anything other than the text in the TextBlock tooltip, you can simply use the inline ad that will generate the TextBlock for you and does not require any hacking to get around the name resolution problem that you encounter otherwise:

 <Button ... ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=ToolTipText}">... 

You can alternately set a tooltip on the image and use the control as a DataContext, which affects the problem of defining a name. The DataContext will be passed to the ToolTip, which will allow normal binding:

 <Image DataContext="{Binding ElementName=this}" Source="{Binding Source}"> <Image.ToolTip> <TextBlock FontSize="18" Text="{Binding Path=ToolTipText}" /> </Image.ToolTip> </Image> 

This method allows you to add additional settings to TextBlock or more complex visual effects.

0
source

This fixes the problem using the binding properties and tooltip dependencies:

 <UserControl x:Class="Extended.InputControls.TextBoxUserControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:Extended.InputControls" x:Name="UserControl" DataContext="{Binding RelativeSource={RelativeSource Self}}"> <TextBox x:Name="textBox"> <TextBox.ToolTip> <ToolTip Content="{Binding Path=CustomToolTip}" Background="Yellow"/> </TextBox.ToolTip> </TextBox> </UserControl> 

Instead (does not Work):

 <UserControl x:Class="Extended.InputControls.TextBoxUserControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:Extended.InputControls" x:Name="UserControl"> <TextBox x:Name="textBox"> <TextBox.ToolTip> <ToolTip Content="{Binding ElementName=UserControl, Path=CustomToolTip}" Background="Yellow"/> </TextBox.ToolTip> </TextBox> </UserControl> 
0
source

All Articles