Answered by:
ListView cell tooltip when text won't fit column width

Question
-
Is there any way to easily get a tooltip displayed for a cell within a ListView that implements a GridView, when the column is too narrow to display all the cell contents (i.e. similar to the ShowItemToolTips property for the Windows Forms ListView) ?
Friday, June 16, 2006 8:40 AM
Answers
-
you have to create celltemplate for the column and then specify what to show in the tooltip
<DataTemplate x:Key="dt1">
<TextBlock Text="{Binding XPath=@CustomerID}">
<TextBlock.ToolTip >
<ToolTip>
<TextBlock Text="{Binding XPath=@CompanyName}"></TextBlock>
</ToolTip>
</TextBlock.ToolTip>
</TextBlock>
</DataTemplate>
Friday, June 16, 2006 1:37 PM
All replies
-
you have to create celltemplate for the column and then specify what to show in the tooltip
<DataTemplate x:Key="dt1">
<TextBlock Text="{Binding XPath=@CustomerID}">
<TextBlock.ToolTip >
<ToolTip>
<TextBlock Text="{Binding XPath=@CompanyName}"></TextBlock>
</ToolTip>
</TextBlock.ToolTip>
</TextBlock>
</DataTemplate>
Friday, June 16, 2006 1:37 PM -
I was hoping that there would be some automatic way of working out whether the cell content would fit, and then display the tooltip only if it wasn't all visible (as the ShowItemToolTips property did in WinForms).
Friday, June 16, 2006 2:08 PM -
Bump - I have a similar need in a TreeView. I'd like each treeviewitem text to display a tooltip only if the text is clipped by the parent container. Any solution?
- Proposed as answer by GooeyIsMe Wednesday, June 25, 2008 7:05 PM
Friday, May 23, 2008 2:56 PM -
I had some success with this, I define a hierarchical data template to customize the look of the hierarchical data that I've set with the TreeView ItemsSource property.
In the HierarchicalDataTemplate for my TreeView, I have a TextBlock that displays the text for the item as follows:
<!-- Bulk of the datatemplate is missing -->
<TextBlock x:Name="FolderText" Grid.Column="1" Margin="4,0,0,0" Text="{Binding Path=ItemName}" ToolTipOpening="OnItemToolTipOpening">
<TextBlock.ToolTip>
<ToolTip Content="{Binding Path=ItemName, Mode=Default}" Placement="Relative" VerticalOffset="0.0"
HorizontalOffset="0.0" PlacementTarget="{Binding ElementName=FolderText}" />
</TextBlock.ToolTip>
</TextBlock>
The code for OnItemToolTipOpening
void OnItemToolTipOpening(object sender, ToolTipEventArgs e)
{
TextBlock item = sender as TextBlock;
if (item != null)
{
// Determine right edge of text with respect to the TreeView.
// If it is within TreeView bounds, then suppress the tooltip.
//
Point itemScreenPosition = item.PointToScreen(new Point(0, 0));
Point itemTreePosition = DirectoryTree.PointFromScreen(itemScreenPosition);
double itemExtent = itemTreePosition.X + item.ActualWidth;
if ( itemExtent < DirectoryTree.ActualWidth)
{
e.Handled = true;
}
}
}
At least this is one option that appears to work.- Proposed as answer by GooeyIsMe Tuesday, July 8, 2008 6:45 PM
Wednesday, June 25, 2008 7:20 PM