scott lafoy
scott lafoy

Reputation: 1013

How to databind the tooltip to part of a user control?

I know this question has been asked many times before, I have found a few solutions here on line, but I could not make any of them work. I have a few elements on a custom control I have, and I want to put a bound tool tip on one of them. This is what I have so far.

<UserControl x:Class="DirectGraphicsControl.ColorBarView"
         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" 
         xmlns:DirectGraphicsControl="clr-namespace:Windows.DirectGraphicsControl" mc:Ignorable="d" 
         SizeChanged="UserControlSizeChanged" MinWidth="95">
<DockPanel LastChildFill="True" Width="80" x:Name="_mainDockPanel">
    <TextBlock DockPanel.Dock="Top" x:Name="_title" x:FieldModifier="public" HorizontalAlignment="Center" Margin="0,2,0,2"/>
    <StackPanel Orientation="Vertical" DockPanel.Dock="Bottom" Margin="0,2,0,2"
                    x:Name="_bottomLabelsRegion" x:FieldModifier="public">
        <TextBlock x:Name="_unitName" x:FieldModifier="public" HorizontalAlignment="Center" VerticalAlignment="Bottom"/>
    </StackPanel>
    <Grid DataContext="{Binding ElementName=_mainDockPanel.Parent, Path=MyTooltip}" >
        <DirectGraphicsControl:DirectGraphicsControl x:Name="_directGraphicsControl"
                                                     MouseDoubleClick="HandleColorBarMouseDoubleClick" x:FieldModifier="public">
            <DirectGraphicsControl:DirectGraphicsControl.ToolTip>
                <ToolTip Content="{Binding Path=PlacementTarget.DataContext, 
                                   RelativeSource={RelativeSource Self}}"/>
            </DirectGraphicsControl:DirectGraphicsControl.ToolTip>
        </DirectGraphicsControl:DirectGraphicsControl>
    </Grid>
</DockPanel>

and this is the code behind.

/// <summary>
    /// The tool tip text
    /// </summary>
    public static readonly DependencyProperty TooltipProperty =
        DependencyProperty.Register(Reflection.GetPropertyName<ColorBarView>(m => m.MyTooltip),
                                    typeof(string), typeof(ColorBarView));

    /// <summary>
    /// The tool tip text
    /// </summary>
    public string MyTooltip
    {
        get { return "Test from binding"; }
    }

The code behind property is only returning a hard coded string for now until I can actually get it working and then it will return the calculated value I would like it to. Currently on mouse over an empty box appears. When I add the text for the tooltip straight to the xaml it shows fine. This makes me think it cannot find the location of the binding.

I am very new to wpf so any suggestions would be great.

Thank you,

Upvotes: 3

Views: 634

Answers (2)

tillerstarr
tillerstarr

Reputation: 2636

Try changing the dependency property to:

 public static readonly DependencyProperty TooltipProperty =
    DependencyProperty.Register(Reflection.GetPropertyName<ColorBarView>(m => m.MyTooltip),
                                typeof(string), typeof(ColorBarView),new PropertyMetadata("Test Value") );

and the binding to:

<Grid DataContext="{Binding ElementName=_myControl, Path=MyTooltip}" >

Upvotes: 1

tillerstarr
tillerstarr

Reputation: 2636

Look in the Visual Studio output tab. XAML Binding errors are printed out there, and it should be easy to spot the trouble from there.

Upvotes: 1

Related Questions