【问题标题】:DataGridTextColumn tooltip issueDataGridTextColumn 工具提示问题
【发布时间】:2019-02-02 13:24:04
【问题描述】:

我有这种样式来向数据网格的每一行中的单元格添加工具提示。问题是所有工具提示都具有相同的文本(第一行的文本)。我做错了什么?

        <DataGridTextColumn Header="Profit (%)"
                            Binding="{Binding Percentage, StringFormat=N8}">
            <DataGridTextColumn.CellStyle>
                <Style TargetType="{x:Type DataGridCell}"
                       BasedOn="{StaticResource {x:Type DataGridCell}}">
                    <Setter Property="ToolTip">
                        <Setter.Value>
                            <ToolTip>
                                <ToolTip.ContentTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding Content.Text, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridCell}}}" />
                                    </DataTemplate>
                                </ToolTip.ContentTemplate>
                            </ToolTip>
                        </Setter.Value>
                    </Setter>
                </Style>
            </DataGridTextColumn.CellStyle>
        </DataGridTextColumn>

但如果我使用简单的工具提示,它就完美了

<DataGridTextColumn Header="Profit (%)"
                    Binding="{Binding Percentage, StringFormat=N8}">
    <DataGridTextColumn.CellStyle>
        <Style TargetType="{x:Type DataGridCell}"
               BasedOn="{StaticResource {x:Type DataGridCell}}">
            <Setter Property="ToolTip"
                    Value="{Binding Percentage}" />
        </Style>
    </DataGridTextColumn.CellStyle>
</DataGridTextColumn>

【问题讨论】:

    标签: wpf xaml


    【解决方案1】:

    摆脱ContentTemplate 并将Tooltip 设置为绑定到父DataGridCellTextBlock

    <DataGridTextColumn.CellStyle>
        <Style TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource {x:Type DataGridCell}}">
            <Setter Property="ToolTip">
                <Setter.Value>
                    <ToolTip>
                        <TextBlock Text="{Binding PlacementTarget.Content.Text, 
                                RelativeSource={RelativeSource AncestorType=ToolTip}}" />
                    </ToolTip>
                </Setter.Value>
            </Setter>
        </Style>
    </DataGridTextColumn.CellStyle>
    

    【讨论】:

      猜你喜欢
      • 2011-06-17
      • 2020-07-18
      • 2014-11-23
      • 2021-12-13
      • 2010-11-12
      • 2023-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多