【问题标题】:C# WPF Tooltips not showing on child gridC# WPF 工具提示未显示在子网格上
【发布时间】:2011-05-25 11:59:40
【问题描述】:

由于某种原因,工具提示不会显示在我的子网格中。

<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 目前没有产生任何实质性的东西。

谢谢,

【问题讨论】:

  • 我已经尝试了您的代码,但没有发现任何问题。工具提示按预期显示。图像和文本控件显示各自的工具提示
  • 我重新检查了源文件,发现我忘了提到源 xaml 以“ResourceDictionary”开头,页面本身是“DataTemplate”,不确定这是否重要,很明显但确实如此。后端框架旨在使用这些数据模板并生成所需的页面,除了子子网格外,它可以完美运行。

标签: wpf tooltip


【解决方案1】:

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

在面板的情况下,这是通过将背景颜色设置为 null 以外的颜色来完成的(这是所有面板的默认值)。如果您希望面板不可见但仍符合命中测试条件,您可以使用Transparent 作为背景颜色。

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

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

【讨论】:

  • 感谢 Isak,我已经尝试过您上面提出的后台方法但没有成功,我什至将其更改为红色,以显示确切的网格行,但仍然没有工具提示。叹。我在上面为 biju 留下了评论,并附上了一些额外的细节,也许这会影响它?
【解决方案2】:

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

我最终扔掉了定义行/列的网格,转而使用嵌套 StackPanel 的网格。

以前 Grid.Rows 由 ContentControls 填充,我将其替换为包含我需要显示的信息的本地 Stackpanels,这似乎已经解决了它,但只有在我将“透明”标签添加到堆栈面板之后一个

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>

如果您遇到类似的事情,希望这对其他人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-04
    • 2020-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-08
    相关资源
    最近更新 更多