DataGridCell的ToolTip发生值消失

时间:2014-07-23 14:19:28

标签: c# wpf styles wpfdatagrid

我的DataGridCell样式中的ToolTip存在问题。当我尝试在工具提示中显示单元格的内容时,此内容会消失。我必须在每个单元格上显示此工具提示,并且我会以dinamically方式生成列,因此我无法绑定到任何属性名称。这是我的片段:

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

有没有人有任何想法? 感谢

1 个答案:

答案 0 :(得分:0)

如果您要将工具提示添加到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>

或者您可以浏览Link

更新: -

如果您动态生成列,则可以在网格上处理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!!!**
}