Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Binding to Tooltip

Tags:

c#

binding

wpf

xaml

Not sure whats doing here, but the binding works for the label in the data template but not the tool tip. Any help will be appreciated.

                    <DataTemplate DataType="Label">
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
                        <StackPanel.ToolTip>
                            <ToolTip DataContext="{Binding Path=PlacementTarget,
       RelativeSource={x:Static RelativeSource.Self}}">
                                <TextBlock Text="{Binding Path=DataContext.Description}" />
                            </ToolTip>
                        </StackPanel.ToolTip>
                        <Image Source="{StaticResource ApplicationInfoS}" 
                               Margin="0 0 5 0" Stretch="None"
                               HorizontalAlignment="Left" />
                        <Label Style="{StaticResource lblTextContent}" 
                               Padding="5 0 0 0"
                               Content="{Binding Path=DataContext.Description, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}"/>
                    </StackPanel>
                </DataTemplate>

BTW the DataTemplate is used in Listview. "Description" property exists on the view model bound to the list view.

The message I get in the output window in VS2010 is:

System.Windows.Data Error: 39 : BindingExpression path error: 'Description' property not found on 'object' ''String' (HashCode=-466763399)'. BindingExpression:Path=DataContext.Description; DataItem='StackPanel' (Name=''); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

UPDATE

I have given up for now. Using the following hack for the time being:

Add a Tag to the StackPanel and Bind "Description" to it

    <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Tag="{Binding Path=DataContext.Description, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}">

Bind the ToolTip to the Tag. Yes a hack but it works.

<StackPanel.ToolTip>
  <ToolTip DataContext="{Binding Path=PlacementTarget, RelativeSource={x:Static RelativeSource.Self}}">
    <TextBlock Text="{Binding Path=Tag}" />
  </ToolTip>
</StackPanel.ToolTip>

Cheers

Mike

like image 781
ozczecho Avatar asked Nov 03 '10 03:11

ozczecho


People also ask

How to bind ToolTip in WPF?

Instead, what you have to do is create a specific instance of a ToolTip, and assign it a style that sets its DataContext (very important; that's how you can bind to the properties of the data source of its "placement target," i.e. the control that's displaying the tooltip) and its Template .

How do I add ToolTip to XAML?

Use the Placement property or ToolTipService. Placement attached property to place the ToolTip above, below, left, or right of the pointer. You can set the VerticalOffset and HorizontalOffset properties to change the distance between the pointer and the ToolTip.


1 Answers

Tooltip should not need a relativesource binding. Try this with no data context binding

<StackPanel.ToolTip>
    <ToolTip Content={Binding Description} />
<StackPanel.ToolTip>

This is assuming that the Label type that this DataTemplate is based on has a property called Description. One of the few instances where you may need to bind to the PlacementTarget is in some cases with a ContextMenu control.

like image 117
TerrorAustralis Avatar answered Oct 14 '22 07:10

TerrorAustralis