Creating DataGrid tooltips for only one column (different tooltip for each row)

Viewed 384

I would like to create tooltips for my datagrid, but only for the first column, with every row on the column having a different tooltip text.

As my datagrid can display different kind of datasets (user selecting the right one using combobox), I think that the tooltips should be created in code-behind (instead of using some kind of binding)?

Update:

XAML:

<DataGrid x:Name="DG_ConfigWindow" Height="253" Margin="0,37,0,0" VerticalAlignment="Top" Loaded="DG_ConfigWindow_Loaded" 
    CellEditEnding="DG_ConfigWindow_CellEditEnding" IsReadOnly="True" HorizontalAlignment="Left" Width="705" 
    PreviewKeyDown="DG_ConfigWindow_KeyDown" SelectionMode="Single" CanUserSortColumns="False" CanUserReorderColumns="False"> 
    <DataGrid.Resources>
        <Style TargetType="DataGridCell">
            <EventSetter Event="MouseEnter" Handler="DG_ConfigWindow_MouseEnter"/>
            <Setter Property="ToolTip" Value="{Binding Path=TooltipText, UpdateSourceTrigger=PropertyChanged, Mode=OneWay}"/>
        </Style>
    </DataGrid.Resources>
</DataGrid>

Code:

private string _TooltipText;
public string TooltipText
{
    get { return _TooltipText; }
    set
    {
        _TooltipText = value;
        NotifyPropertyChanged();
    }
}

private void DG_ConfigWindow_MouseEnter(object sender, MouseEventArgs e)
{
    TooltipText = "test";
}

INotifyPropertyChanged:

public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

And my class is defined like this:

public partial class ConfigWindow : Window, INotifyPropertyChanged
{ ... }
1 Answers

This example shows you how to set up a ToolTip. In this case based on your need you can update the variable that I called YourText from code behind and change the ToolTip.

<DataGrid.ToolTip>
    <ToolTip Background="#FAFAFA">
         <FlowDocumentScrollViewer ScrollViewer.VerticalScrollBarVisibility="Hidden" MaxWidth="250" >
             <FlowDocument FontSize="12" LineHeight="16" FontFamily="Sagoe UI" IsOptimalParagraphEnabled="True" IsHyphenationEnabled="True">
                  <Paragraph>
                     <Run Text="{Binding Path=YourText, UpdateSourceTrigger=PropertyChanged}" />
                   </Paragraph>
              </FlowDocument>
         </FlowDocumentScrollViewer>
    </ToolTip>
</DataGrid.ToolTip>

Update:

Here is a way to get the element of DataGrid when you do MouseOver

First add an EventSetter in Style like below:

   <DataGrid.Resources>
         <Style TargetType="{x:Type DataGridCell}">
              <EventSetter Event="MouseEnter" Handler="EventSetter_OnHandler"/>
         </Style>
  </DataGrid.Resources>

And the Handler should be like this:

private void EventSetter_OnHandler(object sender, MouseEventArgs e)

    {
        DataGridCell dgc = sender as DataGridCell;

        TextBox tb = Utils.GetChildOfType<TextBox>(dgc);
        //I assumed you have TextBox for your cell. After finding the element you then need to implement your logic here to update the YourText variable here.

    }

This helper will help you find your cell

public static T GetChildOfType<T>(DependencyObject depObj) where T : DependencyObject
    {
        if (depObj == null) return null;

        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
        {
            var child = VisualTreeHelper.GetChild(depObj, i);

            var result = (child as T) ?? GetChildOfType<T>(child);
            if (result != null) return result;
        }
        return null;
    }
Related