11

I have DataGrid and one of the DataGrid columns looks like this

<DataGridTextColumn Header="Value" 
        Binding="{Binding Value, Converter={StaticResource BooleanToYesNoConverter}}" 
        x:Name="_col2" 
        IsReadOnly="True"
        CanUserSort="False"
        Width="*">
    <DataGridTextColumn.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="ToolTip" Value="{Binding Value, Converter={StaticResource BooleanToYesNoConverter}}" />
        </Style>
    </DataGridTextColumn.CellStyle>
</DataGridTextColumn>

The problem is I forced to use BooleanToYesNoConverter converter twice. It means that Convert method of BooleanToYesNoConverter will be invoked twice. Therefore, I want to optimize my code. And want to bind value of ToolTip property directly to value of cell.

I tried approach with using ElementName-s. But I have no idea what should I specify in Path property of binding.

<DataGridTextColumn Header="Value" 
        Binding="{Binding Value, Converter={StaticResource BooleanToYesNoConverter}}" 
        x:Name="_col2" 
        IsReadOnly="True"
        CanUserSort="False"
        Width="*">
    <DataGridTextColumn.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="ToolTip" Value="{Binding ElementName=_col2, Path=???}" />
        </Style>
    </DataGridTextColumn.CellStyle>
</DataGridTextColumn>

I tried to use DataGridTemplateColumn instead of DataGridTextColumn, but it does't work too.

<DataGridTemplateColumn CanUserSort="False"
                        Header="Значение"
                        Width="*">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Value, Converter={StaticResource BooleanToYesNoConverter}}"
                        Name="_textBlock"/>    
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
    <DataGridTemplateColumn.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="ToolTip" Value="{Binding RelativeSource ElementName=_textBlock, Path=Text}" />
        </Style>
    </DataGridTemplateColumn.CellStyle>
</DataGridTemplateColumn>

How can I solve my task. Is it possible at all?

monstr
  • 1,680
  • 1
  • 25
  • 44

2 Answers2

27

Use this Style :

<Style TargetType="DataGridCell">
    <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self},Path=Content.Text}"/>
 </Style>
Amol Bavannavar
  • 2,062
  • 2
  • 15
  • 36
  • Btw, `Content` is property type of `Object`. `Object` has no `Text` property. Do you know what type of object has a `Text` property? In other words, what type `Text` property belongs to? It is interesting and not obviously. – monstr Dec 11 '14 at 13:04
  • Yes you are thinking right. In ContentPresentor of DataGridCell,it has a TextBlock and this TextBlock have Text property. Do you understood? – Amol Bavannavar Dec 11 '14 at 13:26
  • Its not a magic . Just check out its Template you will come to know it. http://msdn.microsoft.com/en-us/library/cc278066(v=vs.95).aspx – Amol Bavannavar Dec 11 '14 at 13:59
  • I already found this link and tried to find out where is `TextBlock` are you tolking about in `ControlTemplate` of `DataGridCell`. But I don't see it. Do you talking about style ` – monstr Dec 11 '14 at 14:05
  • Hey I found it by traversing child's of DataGridCell.. I did manual traversing of DataGridCell on code behind and I found that their is a TextBlock. So I tried directly setting its Content's Text property & it worked. – Amol Bavannavar Dec 11 '14 at 14:16
  • Ha! Then this msdn-link actually useless :) – monstr Dec 11 '14 at 14:25
  • 2
    If you want to apply the ToolTip only to specific columns of the grid, then (1) add a key to the style, e.g. `` – Beauty May 25 '18 at 15:16
1

Try just setting the ToolTip to the DataGridCell's DataContext like so:

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

If you do not get the desired content you can then specify the converter as well:

<DataGridTextColumn.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="ToolTip" Value="{Binding Converter={StaticResource BooleanToYesNoConverter}}" />
        </Style>
</DataGridTextColumn.CellStyle>
Xtr
  • 374
  • 5
  • 12
  • @Down-voter: If you are going to down-vote then you should at LEAST give a reason or provide a better answer. – Xtr Dec 11 '14 at 10:48
  • @ Xtr, I did not downwote your answer, but I should do it. Because, first, Value="{Binding}" - obviously incorrect. Second - question is "how do not use BooleanToYesNoConverter" twice. You should check your advice before answering. – monstr Dec 11 '14 at 11:03
  • @monstr Value="{Binding}" would bind to the inherited DataContext. I did not test my answer before posting, sorry about that. – Xtr Dec 11 '14 at 11:17
  • @monstr I have looked at the problem a bit more closely now. You could get to the DataGridCell's content by binding to Value="{Binding Content, RelativeSource={RelativeSource Self}}", but doing this will detach the content from the cell and assign it to the ToolTip. So apart from setting a converter on that binding to ToString the content you would use your BooleanToYesNoConverter twice. – Xtr Dec 11 '14 at 11:50