#375 – Binding Something in a Tooltip to a Property on the Parent Control

You can use data binding to bind the value of a simple Tooltip property to some other property of the control.  But you might also want to use data binding when you create a Tooltip as a child element in XAML.

Suppose you want a Tooltip to include several labels and you want the Content of the second label to bind back to a property on the parent control.

You can do this by using the PlacementTarget of the Tooltip to find its parent and then set the DataContext of the Tooltip.

        <TextBox Text="Now is the winter of our discontent etc"
            Width="100" Margin="10">
            <TextBox.ToolTip>
                <ToolTip DataContext="{Binding Path=PlacementTarget, RelativeSource={x:Static RelativeSource.Self}}">
                    <StackPanel>
                        <Label FontWeight="Bold" Content="Full Text"/>
                        <Label Content="{Binding Text}"/>
                        <Label Content="--Gloster, in Richard III (Act I, Scene I)"/>
                    </StackPanel>
                </ToolTip>
            </TextBox.ToolTip>
        </TextBox>

Advertisement

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

3 Responses to #375 – Binding Something in a Tooltip to a Property on the Parent Control

  1. Pingback: Dew Drop – August 30, 2011 | Alvin Ashcraft's Morning Dew

  2. Andrew says:

    Thank you, helped me. By the way, RelativeSource={RelativeSource Self} suffice.

  3. tpatel says:

    This, indeed one of the best blogs.Thank you.

    Though, I do have a question for Sean, Can you please guide me in right direction to learn about this whole WPF binding stuff? It really gets confusing when time comes to choose what gets bound to what and what property to choose? It is really difficult to find the whole documentation of these things in one place (not even MSDN).

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 )

Facebook photo

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

Connecting to %s

%d bloggers like this: