#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>

About these ads

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

2 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.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 366 other followers

%d bloggers like this: