Search code examples
wpfbindingtooltip

BindingExpression path error at ToolTip binding


I used ToolTip in DataGridCell. In my project, I used AvalonDock and I created 3 or 4 Window tabs. And then, it works well and also tooltips displayed well in DataGrid, but there are errors when i swapped tabs. I think load procedure...
Here is a part of my .xaml code.

        <DataGrid.CellStyle>
            <Style TargetType="DataGridCell" BasedOn="{StaticResource MetroDataGridCell}">
                    <Setter Property="ToolTip" Value="{Binding Path=Content.Text, RelativeSource={RelativeSource Self}}" />
            </Style>
        </DataGrid.CellStyle>

It worked. But there are errors that say

System.Windows.Data Error: 40 : BindingExpression path error: 'Text' property not found on 'object' ''ContentPresenter' (Name='')'. BindingExpression:Path=Content.Text; DataItem='DataGridCell' (Name=''); target element is 'DataGridCell' (Name=''); target property is 'ToolTip' (type 'Object')

How can I fix it?

+ Addition I modified the source.

<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content}" />

enter image description here

Then, BindingExpression errors were dissapeared. But contents of DataGridCell is dissapear after showing ToolTip like picture I posted!! Strange Bug !!!


Solution

  • I make my ToolTip in this way:

    <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content.Text}"/>
    

    Alternativ

    <DataGrid>
        <DataGrid.Columns>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.Header>
                    <TextBlock Text="Foo Header"/>
                </DataGridTemplateColumn.Header>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <DataTemplate.Resources>
                            <Style TargetType="{x:Type TextBlock}">
                                <Setter Property="ToolTip">
                                    <Setter.Value>
                                        <ToolTip 
                                                Style="{StaticResource ToolTipBrowserDescription}" 
                                                Content="{Binding FooProperty}"
                                                HasDropShadow="True">
                                        </ToolTip>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </DataTemplate.Resources>
                        <TextBlock Text="{Binding FooProperty}" TextTrimming="CharacterEllipsis"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
    

    ToolTipStyle

    <Style TargetType="{x:Type ToolTip}" x:Key="ToolTipBrowserDescription">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ToolTip}">
                    <Border BorderBrush="#FF424242" Background="#FFEEEEEE" BorderThickness="1">
                        <TextBlock Text="{TemplateBinding Content}" FontWeight="Bold" TextWrapping="Wrap" Margin="5" MinWidth="50" MaxWidth="500"/>
                        <Border.Effect>
                            <DropShadowEffect Opacity="0.65" ShadowDepth="1"/>
                        </Border.Effect>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="ToolTipService.HasDropShadow" Value="True">
                            <Setter Property="Margin" Value="0,0,1,1"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    Preview

    preview