【问题标题】:BindingExpression path error at ToolTip bindingToolTip 绑定时的 BindingExpression 路径错误
【发布时间】:2017-03-17 01:43:12
【问题描述】:

我在 DataGridCell 中使用了 ToolTip。 在我的项目中,我使用了 AvalonDock 并创建了 3 或 4 个窗口选项卡。 然后,它运行良好,工具提示在 DataGrid 中也显示良好,但 当我交换标签时出现错误。我认为加载程序...
这是我的 .xaml 代码的一部分。

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

成功了。 但是有错误说

System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“ContentPresenter”(名称=“”)上找不到“文本”属性。绑定表达式:路径=内容.文本; DataItem='DataGridCell' (名称='');目标元素是'DataGridCell'(名称='');目标属性是“工具提示”(类型“对象”)

我该如何解决?

+加法 我修改了源代码。

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

enter image description here

然后,BindingExpression 错误消失了。但是 DataGridCell 的内容在显示 ToolTip 后消失了,就像我发布的图片一样!!奇怪的虫子!!!

【问题讨论】:

    标签: wpf binding tooltip


    【解决方案1】:

    我以这种方式创建我的ToolTip

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

    另类

    <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>
    

    工具提示样式

    <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>
    

    预览

    【讨论】:

    • 这是我第一次尝试的。它运行良好,但出现类似“System.Windows.Data Error: 40: BindingExpression path error: 'Text' property not found on 'object' ''ContentPresenter' (Name='')' 之类的错误。BindingExpression:Path=Content .Text; DataItem='DataGridCell' (Name=''); 目标元素是 'DataGridCell' (Name=''); 目标属性是 'ToolTip' (type 'Object')"。
    • 感谢您的好意回答。我用您的代码进行了修改,然后 ToolTips 显示良好,没有错误。但是这样的 DataGridCells 中的内容不显示! :(
    • 嗯...它需要显示。如何添加图片?
    • 天哪,我弄错了对不起!它工作得很好!非常感谢。 :) 我投票给你
    猜你喜欢
    • 1970-01-01
    • 2021-02-13
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-15
    • 2020-04-22
    • 2015-08-11
    相关资源
    最近更新 更多