C# – WPF Binding to Tooltip

bindingcwpfxaml

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

Best Answer

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.

Related Topic