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
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 .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With