1

I have problem with ToolTip in my DataGridCell style. When I try to show content of cell in tooltip this content disappears. I must show this tooltip on each cell and I generate columns dinamically so I can't bind to any property name. Here is my snippet:

<Style x:Key="dgCellStyle" TargetType="{x:Type Controls:DataGridCell}">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Controls:DataGridCell}">
                    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                        <Grid Background="{TemplateBinding Background}">
                            <ToolTipService.ToolTip>
                                <ContentControl Content="{TemplateBinding Content}" />
                            </ToolTipService.ToolTip>
                            <ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center" Margin="0,2"/>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
                <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
            </Trigger>
            <Trigger Property="IsKeyboardFocusWithin" Value="True">
                <Setter Property="BorderBrush" Value="{DynamicResource {ComponentResourceKey ResourceId=FocusBorderBrushKey, TypeInTargetAssembly={x:Type Controls:DataGrid}}}"/>
            </Trigger>
        </Style.Triggers>
</Style>

Does anyone have any idea? Thanks

Jaume
  • 744
  • 1
  • 7
  • 21
  • You better check [this][1] question. [1]: http://stackoverflow.com/questions/7173430/showing-tool-tip-for-every-item-in-datagridview-row-when-mouse-is-above-it – Vivek Saurav Jul 25 '14 at 07:51
  • Thanks for reply, but the link you have posted is about winforms and i need solution with wpf framework and not by code behind but in xaml way. – Jaume Jul 25 '14 at 08:00

1 Answers1

0

If you want to add tooltip to DataGridTextColumn,

you could use DataGridTextColumn.CellStyle property,

Refer to below code snippet:

<DataGridTextColumn Header="ScreenName" Binding="{Binding Name}" >
    <DataGridTextColumn.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="ToolTip" Value="{Binding Age}" />
        </Style>
    </DataGridTextColumn.CellStyle>
</DataGridTextColumn>

Or you can go through this Link

Update :-

if you're generating columns dynamically you can handle MouseMove event on the grid.

You can then transform coordinates of the mouse to the row handle and change grid's tooltip accordingly.

Something like:

private void dataGrid_MouseMove(object sender, MouseEventArgs e) {
 var point = dataGrid.PointToClient(e.X, e.Y);
 var hittest = dataGrid.HitTest(point.X, point.Y);
 toolTip1.SetToolTip(dataGrid, hittest.Row); // **add Tooltip control to the Application!!!**
}
Vivek Saurav
  • 2,235
  • 4
  • 25
  • 46
  • There is a problem with this way of adding tooltip because i generate columns dinamically, as i wrote in main post, So I can't bind tooltip's text with concrete property. I must use binding to cell's content – Jaume Jul 25 '14 at 08:27
  • Check the updated answer you have to add a tooltip control to the application . – Vivek Saurav Jul 25 '14 at 08:43
  • This what you've posted is for winforms datagrid. I've found way to do this with MouseMove event, but I don't want to do this. In this project I'm using MVVM pattern so I try to avoid codebehind in view file. I wonder why this what i've posted on the start doesn't work because it's one way binding so this shouldn't be problem with show this tooltip. I want to do this only with XAML because it should work but I don't know why it doesn't :) I hope that some person with very good knowledge of XAML can help me with this :) – Jaume Jul 25 '14 at 09:37