将DataGridCell的ToolTip属性绑定到DataGridCell的值

11

我有一个DataGrid,其中一个DataGrid列看起来像这样

<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>
问题在于我被迫两次使用BooleanToYesNoConverter转换器。这意味着BooleanToYesNoConverter的Convert方法将被调用两次。因此,我想优化我的代码,并希望直接将ToolTip属性的值绑定到单元格的值。
我尝试了使用ElementName的方法。但是我不知道在绑定的Path属性中应该指定什么。
<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>

我尝试使用 DataGridTemplateColumn 代替 DataGridTextColumn,但它也不起作用。

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

我该如何解决我的任务?这个任务是否有可能完成?

2个回答

27

使用这种样式:

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

顺便说一下,ContentObject的属性类型。Object没有Text属性。请问您知道什么类型的对象具有Text属性吗?换句话说,Text属性属于哪种类型?这很有趣且不明显。 - monstr
这并不是魔法。只需查看其模板,您就会了解它。http://msdn.microsoft.com/en-us/library/cc278066(v=vs.95).aspx - Amol Bavannavar
嘿,我通过遍历DataGridCell的子元素找到了它。我在代码后台手动遍历了DataGridCell,并发现其中有一个TextBlock。所以我尝试直接设置它的Content的Text属性,结果成功了。 - Amol Bavannavar
哈!那么这个 MSDN 链接实际上是没用的 :) - monstr
2
如果您想将ToolTip仅应用于网格的特定列,则需要(1)向样式添加一个键,例如<Style x:Key="CellWithTooltip" ...>,并且(2)像这样将其应用于所需的列<DataGridTextColumn CellStyle="{StaticResource CellWithTooltip}" ... /> - Beauty
显示剩余2条评论

1

尝试将ToolTip设置为DataGridCell的DataContext,如下所示:

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

如果您没有得到所需的内容,您可以指定转换器:
<DataGridTextColumn.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="ToolTip" Value="{Binding Converter={StaticResource BooleanToYesNoConverter}}" />
        </Style>
</DataGridTextColumn.CellStyle>

如果你要点踩,那么至少应该给出一个理由或提供更好的答案。 - Xtr
@ Xtr,我没有给你的答案投反对票,但我应该这样做。因为首先,Value =“{Binding}”- 显然是不正确的。其次 - 问题是“如何不使用BooleanToYesNoConverter两次”。在回答之前,您应该检查一下自己的建议。 - monstr
@monstr Value="{Binding}" 将绑定到继承的DataContext。在发布之前我没有测试我的答案,抱歉。 - Xtr
@monstr 我现在更仔细地看了一下这个问题。你可以通过将Value绑定到" {Binding Content,RelativeSource={RelativeSource Self}} "来访问DataGridCell的内容,但这样做会将内容从单元格中分离出来并将其分配给ToolTip。因此,除了在该绑定上设置一个转换器以ToString内容之外,您还需要使用BooleanToYesNoConverter两次。 - Xtr

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接