【问题标题】:ToolTip of DataGridCell occurs value disappearingDataGridCell 的 ToolTip 发生值消失
【发布时间】:2014-09-14 18:54:00
【问题描述】:

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

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

有人知道吗? 谢谢

【问题讨论】:

  • 您最好检查 [this][1] 问题。 [1]:stackoverflow.com/questions/7173430/…
  • 感谢您的回复,但您发布的链接是关于 winforms 的,我需要 wpf 框架的解决方案,而不是通过代码,而是通过 xaml 方式。

标签: c# wpf styles wpfdatagrid


【解决方案1】:

如果你想在DataGridTextColumn添加tooltip

你可以使用DataGridTextColumn.CellStyle property

参考下面的代码sn-p:

<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!!!**
}

【讨论】:

  • 这种添加工具提示的方式存在问题,因为我以动态方式生成列,正如我在主帖中所写,所以我无法将工具提示的文本与具体属性绑定。我必须使用绑定到单元格的内容
  • 检查您必须向应用程序添加工具提示控件的更新答案。
  • 您发布的内容是针对 winforms 数据网格的。我已经找到了使用 MouseMove 事件执行此操作的方法,但我不想这样做。在这个项目中,我使用了 MVVM 模式,所以我尽量避免在视图文件中使用代码隐藏。我想知道为什么我在开始时发布的内容不起作用,因为它是一种方式绑定,所以这不应该是显示此工具提示的问题。我只想用 XAML 来做这件事,因为它应该可以工作,但我不知道为什么它不能 :) 我希望一些对 XAML 非常了解的人可以帮助我:)
猜你喜欢
  • 2014-12-26
  • 1970-01-01
  • 1970-01-01
  • 2016-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-22
  • 1970-01-01
相关资源
最近更新 更多