c# - 将 DataGridCell 工具提示属性绑定(bind)到 DataGridCell 的值

标签 c# wpf tooltip datagridcell

我有 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 转换器。这意味着 BooleanToYesNoConverterConvert 方法将被调用两次。因此,我想优化我的代码。并希望将 ToolTip 属性的值直接绑定(bind)到单元格的值。

我尝试使用 ElementName-s 的方法。但是我不知道应该在绑定(bind)的 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>

我怎样才能解决我的任务。有可能吗?

最佳答案

使用这种风格:

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

关于c# - 将 DataGridCell 工具提示属性绑定(bind)到 DataGridCell 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27418403/

相关文章:

javascript - 图像映射中的纯 CSS 数据工具提示(jquery?)

javascript - nvd3 散点图的自定义 x 轴工具提示

c# - OpenXml 电子表格。值过滤器、样式和 unicode 字符

C# 后期绑定(bind)到 com/activeX 服务器,一种方法有问题

c# - 应用程序如何正确崩溃?

c# - 使 VisualTreeHelper.HitTest 在单元测试中工作?

c# - 如何在WPF中实现 "infinite-sized" Canvas 效果?

c# - 如何取消await Task.Delay()?

c# - MVP 调用表单

ruby-on-rails - Ruby on Rails 中的 Bootstrap 工具提示