Want WPF DataGrid tooltip to behave like Excel Cell Comments
This is the behaviour i get from an Excel Cell that has comments:
1) Put mouse anywhere on the cell and the comment pops-up like a yellow tooltip;
2) Comment will stay up indefinitely for as long as mouse cursor remains on the cell
I need similar functionality on the DataGridColumnHeader only.
I started off as follows:
<DataGridTextColumn Binding="{Binding Path=SomeProperty}">
<DataGridTextColumn.Header>
<TextBlock Text="MyColHeader" Background="red" >
<TextBlock.ToolTip>
<StackPanel Width="400">
within stack panel make format as fancy as i like, works great;
</StackPanel>
</TextBlock.ToolTip>
</TextBlock>
</DataGridTextColumn.Header>
</DataGridTextColumn>
Problem with that is tooltip only pops-up when mouse hovers on the text of the column header, i.e. in my example on the text "MyColHeader" and NOT anywhere on the cell.
After another hour i came up with something that address point 1):
<DataGridTextColumn
Header="MyColHeader"
Binding="{Binding Path=SomeProperty}">
<DataGridTextColumn.HeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="ToolTip">
<Setter.Value>
<StackPanel Width="400">
make formatting as fancy as i like, works great;
</StackPanel>
</Setter.Value>开发者_StackOverflow社区
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</DataGridTextColumn.HeaderStyle>
</DataGridTextColumn>
Much better now, remaining question is i need to address point 2), i.e. Make the tooltip stay on indefinitely as long as cursor is over the col header.
You need to set ToolTipService.StaysOpen on the TextColumn as described here: Forcing a WPF tooltip to stay on the screen
精彩评论