Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding DataGridCell ToolTip property to value of DataGridCell

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?

like image 457
monstr Avatar asked Dec 11 '14 08:12

monstr


2 Answers

Use this Style :

<Style TargetType="DataGridCell">
    <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self},Path=Content.Text}"/>
 </Style>
like image 152
Amol Bavannavar Avatar answered Oct 08 '22 01:10

Amol Bavannavar


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>
like image 42
Xtr Avatar answered Oct 08 '22 02:10

Xtr