C# WPF工具提示在子网格上无法显示

5
由于某种原因,我的子网格中的工具提示不会显示。
<Grid DockPanel.Dock="Top"
    Width="300px"
    Height="250px"
    ToolTipService.ToolTip="MainGrid Tooltip">
<Grid.ColumnDefinitions>
    <ColumnDefinition Width="*" />
    <ColumnDefinition Width="1.25*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
    <RowDefinition Height="Auto" />
    <RowDefinition Height="Auto" />
    <RowDefinition Height="Auto" />
    <RowDefinition Height="Auto" />
</Grid.RowDefinitions>

<Grid Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2"
        ToolTip="mygrid tooltip 2">
    <StackPanel Orientation="Horizontal">
        <Image Width="15"
                Source="<<INSERT IMAGE FILE LOCATION HERE>>" 
                ToolTipService.ToolTip="Child Grid Tooltip 1"/>
        <TextBlock Width="80"
                Text="Random Text 2"
                ToolTipService.ToolTip="Child Grid Tooltip 2"/>
        <TextBlock Width="80" 
                Text="Random Text 3"
                ToolTipService.ToolTip="Child Grid Tooltip 3"/>
    </StackPanel>

</Grid>

我一直看到显示“mygrid tooltip 2”的提示,尽管我已经覆盖了其子级的工具提示-它不会显示。
我已经从资源字典中减少了控件模板的复杂性,现在只剩下上面的内容,但仍然没有效果。
非常感谢您提供任何想法,也许您可以提供相关链接以供参考。 我的WPF书和MSDN目前都没有提供任何实质性的信息。
谢谢,

我已经尝试了你的代码,没有发现任何问题。工具提示显示正常。图像和文本控件也分别显示它们自己的工具提示。 - biju
我重新检查了源文件,发现我忘记提到源xaml以“ResourceDictionary”开头,而页面本身是一个“DataTemplate”。不确定这是否有影响,但显然确实有影响。后端框架旨在使用这些数据模板并生成所需的页面,这完美地运行,除了子级网格。<ResourceDictionary> <DataTemplate> <Grid> <Grid Grid.Row="1"> <Grid tooltip=working> <Grid Tooltip=not working> - Neville
2个回答

14

为了让WPF中的某些内容能够显示工具提示,它必须对命中测试可见。

对于面板来说,这可以通过将背景颜色设置为除null之外的颜色来完成(这是所有面板的默认值)。如果您希望面板不可见但仍然有资格进行命中测试,则可以使用Transparent作为背景颜色。

<Grid Background="Transparent" ToolTip="This Will Be Shown">
    <!-- other stuff -->
</Grid>

<Grid ToolTip="This Will NOT Be Shown">
    <!-- other stuff -->
</Grid>

1
谢谢Isak,我已尝试了您上面提出的背景方法,但没有成功,我甚至将其更改为红色以显示确切的网格行,但仍然没有工具提示。 叹气。 我在上面留下了一条评论,附带了一些额外的细节,也许会影响它? - Neville

1

@Isak,谢谢你的Background="Transparent",它帮助我找到了最终解决方案。

最终,我放弃了定义行/列的网格,并改用嵌套的StackPanels的网格。

以前,Grid.Rows是由ContentControls填充的,我用包含所需显示信息的本地Stackpanels替换了它,这似乎解决了问题,但只有在我为Stackpanels添加了"Transparent"标签和一个

IsHitTestVisible="False"

在父网格中的图像上,它作为背景图像。

这是我目前解决方案的示例,第二部分替换了我原始帖子中看到的代码。

首先,在涉及的代码之前,基本的源文件布局如下:

<ResourceDictionary xmlns="...
  <DataTemplate DataType="...
    <Grid Style="...
      <Grid Grid.Row="1">
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="Auto"/>
          <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

       <Grid Name="LeftColumn" Grid.Column="0">
         <TextBlock Style="{StaticResource TextHeader}"
                    Text="Review 10 Patients"/>

         <Image RenderOptions.BitmapScalingMode="NearestNeighbor"
                SnapsToDevicePixels="True"
                Stretch="None"
                DockPanel.Dock="Top"
                IsHitTestVisible="False"
                Source="((INSERT IMAGE HERE))" Margin="0,-2,0,10"/>

然后我的解决方案网格如下所示 [取代了初始发布的代码]:

       <StackPanel>

         <StackPanel Background="Transparent" Orientation="Horizontal">
           <Image Style="{StaticResource InfoImage}" Margin="3">
             <ToolTipService.ToolTip>
               <ToolTip Width="200" Style="{StaticResource ToolTip}"
                        Content="ToolTip 1"/>
             </ToolTipService.ToolTip>
           </Image>

           <TextBlock Width="140" Style="{StaticResource Text_SelfTitle}"
                      Text="Text Field 1"/>
           <TextBlock Width="145" Style="{StaticResource Text_SelfQuestions}"
                      Text="Text Field 2"/>
         </StackPanel>

         <StackPanel Background="Transparent" Orientation="Horizontal">
            ((... similar as above...))
         </StackPanel>

         <StackPanel Background="Transparent" Orientation="Horizontal">
            ((... similar as above...))
         </StackPanel>

       </StackPanel>
     </Grid>
  </Grid>

希望这能帮助到其他人,如果你遇到类似的问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接