Binding DataGridCell ToolTip property to value of DataGridCell
Asked Answered
E

2

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?

Equerry answered 11/12, 2014 at 8:31 Comment(0)
A
27

Use this Style :

<Style TargetType="DataGridCell">
    <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self},Path=Content.Text}"/>
 </Style>
Apothegm answered 11/12, 2014 at 12:23 Comment(7)
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.Equerry
Yes you are thinking right. In ContentPresentor of DataGridCell,it has a TextBlock and this TextBlock have Text property. Do you understood?Apothegm
Its not a magic . Just check out its Template you will come to know it. msdn.microsoft.com/en-us/library/cc278066(v=vs.95).aspxApothegm
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 <Style TargetType="local:DataGridCell">? Could you copy-paste that important part of style? You can add it to your answer as update.Equerry
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.Apothegm
Ha! Then this msdn-link actually useless :)Equerry
If you want to apply the ToolTip only to specific columns of the grid, then (1) add a key to the style, e.g. <Style x:Key="CellWithTooltip" ...> and (2) apply it to the wanted columns like this <DataGridTextColumn CellStyle="{StaticResource CellWithTooltip}" ... />.Letreece
B
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>
Balbo answered 11/12, 2014 at 10:19 Comment(4)
@Down-voter: If you are going to down-vote then you should at LEAST give a reason or provide a better answer.Balbo
@ 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.Equerry
@Equerry Value="{Binding}" would bind to the inherited DataContext. I did not test my answer before posting, sorry about that.Balbo
@Equerry 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.Balbo

© 2022 - 2024 — McMap. All rights reserved.