Search code examples
wpfdatagrid

Wpf DataGrid ToolTip for column headers


I have a tooltip for datagrid cells which is open when cell content is trimmed:

    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?


Solution

  • So, i finded a solution. I just replaced ContentPresenter with my textblock with custom tooltip in default DataGridHeader style:

    <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.