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

Nev*_*lle 4 wpf tooltip

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

<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>
Run Code Online (Sandbox Code Playgroud)

我一直在显示"mygrid工具提示2",即使我已经覆盖了它的孩子的工具提示 - 它也不会显示.

我已经减少了在资源词典中使用控件模板的复杂性,直到我现在只剩下这个,而且仍然没有.

任何想法将不胜感激,也许我可以阅读它的链接.我的WPF书籍和msdn目前没有产生任何相关的资金.

谢谢,

Isa*_*avo 12

为了使WPF中的某些内容能够显示工具提示,必须能够看到测试.

在面板的情况下,这是通过将背景颜色设置为除此之外的东西null(这是所有面板的默认颜色)来完成的.如果您希望面板不可见但仍有资格进行命中测试,则可以将其Transparent用作背景颜色.

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

<Grid ToolTip="This Will NOT Be Shown">
    <!-- other stuff -->
</Grid>
Run Code Online (Sandbox Code Playgroud)


Nev*_*lle 1

@Isak,谢谢,你Background="Transparent"帮助我解决了最终的问题。

我最终抛弃了具有定义的行/列的网格,并采用了具有嵌套 StackPanel 的网格。

以前 Grid.Rows 是由 ContentControls 填充的,我用包含我需要显示的信息的本地 Stackpanels 替换了它,这似乎已经解决了这个问题,但只有在我向 stackpanels 添加“透明”标签以及一个

IsHitTestVisible="False"
Run Code Online (Sandbox Code Playgroud)

在用作背景图像的父网格中的图像上。

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

首先,进入相关代码之前的基本源文件布局如下所示:

<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"/>
Run Code Online (Sandbox Code Playgroud)

然后我的解决方案网格如下[替换初始后置代码]:

       <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>
Run Code Online (Sandbox Code Playgroud)

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