What is the proper method for binding ToolTip Content using RelativeSource on a custom control?

299 views Asked by At

As per the requisite "Minimal, Complete and Verifiable" -

This is the "Custom Control" to reproduce the issue.

XAML :

<TextBlock
    x:Class="FooTips.FooTipControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:l="clr-namespace:FooTips"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    d:DesignHeight="300" d:DesignWidth="300" mc:Ignorable="d"
    Text="{Binding FooTipText, RelativeSource={RelativeSource Self}}">
    <TextBlock.ToolTip>
        <ToolTip Content="{Binding FooTipText, RelativeSource={RelativeSource AncestorType={x:Type l:FooTipControl}, Mode=FindAncestor}}" />
    </TextBlock.ToolTip>
</TextBlock>

Code :

using System.Windows;

namespace FooTips {
    /// <summary>
    /// Interaction logic for FooTipControl.xaml
    /// </summary>
    public partial class FooTipControl {
        public static readonly DependencyProperty
            FooTipTextProperty = DependencyProperty.Register(
                "FooTipText", typeof( string ), typeof( FooTipControl ) );

        public FooTipControl( ) {
            InitializeComponent( );
        }

        public string FooTipText {
            get => this.GetValue( FooTipTextProperty ).ToString( );
            set => this.SetValue( FooTipTextProperty, value );
        }
    }
}

Drop into a window ( or whatever ) and set FooTipText = "FOO" ( or whatever ).

The text displays in the control, but the build output displays the following binding error -

System.Windows.Data Error: 4 :
    Cannot find source for binding with reference
        'RelativeSource FindAncestor,
        AncestorType='FooTips.FooTipControl',
        AncestorLevel='1''.
    BindingExpression:
        Path=FooTipText;
        DataItem=null;
        target element is 'ToolTip' (Name='');
        target property is 'Content' (type 'Object')

How can I adjust my Tooltip binding so that it will pick up on the control property?

1

There are 1 answers

0
mm8 On BEST ANSWER

This works:

<TextBlock x:Class="FooTips.FooTipControl"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:l="clr-namespace:FooTips"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            d:DesignHeight="300" d:DesignWidth="300" mc:Ignorable="d"
            Text="{Binding FooTipText, RelativeSource={RelativeSource Self}}"
            ToolTip="{Binding FooTipText, RelativeSource={RelativeSource Self}}">
</TextBlock>

...and this too:

<TextBlock.ToolTip>
    <ToolTip Content="{Binding PlacementTarget.FooTipText, RelativeSource={RelativeSource Self}}" />
</TextBlock.ToolTip>