DataGridCell的工具提示出现值消失



我有问题的工具提示在我的DataGridCell风格。当我试图在工具提示中显示单元格的内容时,该内容消失了。我必须在每个单元格上显示此工具提示,并且动态生成列,因此不能绑定到任何属性名称。下面是我的代码片段:

<Style x:Key="dgCellStyle" TargetType="{x:Type Controls:DataGridCell}">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Controls:DataGridCell}">
                    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                        <Grid Background="{TemplateBinding Background}">
                            <ToolTipService.ToolTip>
                                <ContentControl Content="{TemplateBinding Content}" />
                            </ToolTipService.ToolTip>
                            <ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center" Margin="0,2"/>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
                <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
            </Trigger>
            <Trigger Property="IsKeyboardFocusWithin" Value="True">
                <Setter Property="BorderBrush" Value="{DynamicResource {ComponentResourceKey ResourceId=FocusBorderBrushKey, TypeInTargetAssembly={x:Type Controls:DataGrid}}}"/>
            </Trigger>
        </Style.Triggers>
</Style>

有人知道吗?由于

如果您想将工具提示添加到DataGridTextColumn

可以用DataGridTextColumn.CellStyle property

参考下面的代码片段:

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

或者你可以通过这个链接

更新:-

如果你动态生成列,你可以在网格上处理MouseMove事件。

然后,您可以将鼠标的坐标转换为行句柄,并相应地更改网格的工具提示。

Something like:

private void dataGrid_MouseMove(object sender, MouseEventArgs e) {
 var point = dataGrid.PointToClient(e.X, e.Y);
 var hittest = dataGrid.HitTest(point.X, point.Y);
 toolTip1.SetToolTip(dataGrid, hittest.Row); // **add Tooltip control to the Application!!!**
}

相关内容

  • 没有找到相关文章

最新更新