[英]Wpf DataGrid ToolTip for column headers
I have a tooltip for datagrid cells which is open when cell content is trimmed:我有一个 datagrid 单元格的工具提示,当单元格内容被修剪时它会打开:
private void grid2_MouseMove(object sender, MouseEventArgs e)
{
DependencyObject dep = (DependencyObject)e.OriginalSource;
while ((dep != null) &&
!(dep is DataGridCell) && !(dep is DataGrid))
{
if (dep is Run)
continue;
dep = VisualTreeHelper.GetParent(dep);
}
if (dep == null)
return;
if (dep is DataGridCell)
{
DataGridCell cell = dep as DataGridCell;
var x = cell.Content as TextBlock;
if (x.Text != check)
tlp.IsOpen = false;
if (x != null)
{
FormattedText formattedText = new FormattedText(x.Text,
System.Threading.Thread.CurrentThread.CurrentCulture,
FlowDirection.LeftToRight,
new Typeface(x.FontFamily.ToString()),
x.FontSize,
Brushes.Black);
if (formattedText.Width > Convert.ToDouble(cell.Column.ActualWidth.ToString()) - 3)
{
tlp.Content = x.Text;
tlp.IsOpen = true;
check = x.Text;
}
}
}
if (dep is DataGrid)
if (tlp.IsOpen == true)
tlp.IsOpen = false;
}
I want to do the same for column headers, but i don't know how.我想对列标题做同样的事情,但我不知道如何。 There is no such object like "DataGridColumnHeader" for example... Help me, please?例如,没有像“DataGridColumnHeader”这样的对象...请帮帮我好吗?
Here are two ways for adding a tooltip to a column header.以下是向列标题添加工具提示的两种方法。
<DataGrid x:Name="TheDataGrid">
<DataGrid.Columns>
<DataGridTextColumn>
<DataGridTextColumn.Header>
<TextBlock Text="Test">
<TextBlock.ToolTip>
This is a Tooltip
</TextBlock.ToolTip>
</TextBlock>
</DataGridTextColumn.Header>
</DataGridTextColumn>
<DataGridTextColumn>
<DataGridTextColumn.Header>
<TextBlock Text="Test2" MousEnter="TextBlock_MouseEnter"/>
</DataGridTextColumn.Header>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
// mouse enter handler
private void TextBlock_MouseEnter(object sender, MouseEventArgs e)
{
var column = TheDataGrid.Columns[1];
var header = column.Header as TextBlock;
if (headerIsTrimmed ...) // your logic to check for trim
{
header.ToolTip = "This is another Tooltip";
}
else
{
header.ToolTip = null;
}
}
Notice the two DataGridTextColumns, for the first I set the tooltip directly.注意两个 DataGridTextColumns,第一个我直接设置了工具提示。 But note that you have to set a TextBlock as the Header, where you can define the tooltip in XAML.但请注意,您必须将 TextBlock 设置为 Header,您可以在其中定义 XAML 中的工具提示。 On the second DataGridTextColum I also set the Header as a TextBlock, so that in code-behind or an event handler the tooltip can be added to the TextBlock and so the column header.在第二个 DataGridTextColum 上,我还将 Header 设置为 TextBlock,以便在代码隐藏或事件处理程序中可以将工具提示添加到 TextBlock 和列标题。
EDIT: I added an event handler to the MouseEnter event on the TextBlock that is used as a Header (the second one) there you can place your logic to set the Tooltip to a string if you detect a trimming or set it to null if not.编辑:我在用作标题(第二个)的 TextBlock 上的 MouseEnter 事件中添加了一个事件处理程序,如果检测到修剪,则可以放置逻辑以将工具提示设置为字符串,否则将其设置为 null .
So, i finded a solution.所以,我找到了一个解决方案。 I just replaced ContentPresenter with my textblock with custom tooltip in default DataGridHeader style:我刚刚用默认 DataGridHeader 样式中的自定义工具提示将 ContentPresenter 替换为我的文本块:
<TextBlock Text="{Binding}" TextTrimming="CharacterEllipsis">
<TextBlock.ToolTip>
<ToolTip Visibility="{Binding RelativeSource={RelativeSource Self}, Path=PlacementTarget, Converter={StaticResource trimmedVisibilityConverter}}">
<ToolTip.Content>
<TextBlock Text="{Binding}"/>
</ToolTip.Content>
</ToolTip>
</TextBlock.ToolTip>
</TextBlock>
And converter:和转换器:
public class TrimmedTextBlockVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null) return Visibility.Collapsed;
FrameworkElement textBlock = (FrameworkElement)value;
textBlock.Measure(new System.Windows.Size(Double.PositiveInfinity, Double.PositiveInfinity));
if (((FrameworkElement)value).ActualWidth < ((FrameworkElement)value).DesiredSize.Width)
return Visibility.Visible;
else
return Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
I don't know it is good solution or not, but it works exactly like i need... Thanks everyone for the answers.我不知道这是否是好的解决方案,但它的工作原理完全符合我的需要...感谢大家的回答。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.