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

发布于 2024-11-08 23:57:15 字数 1366 浏览 0 评论 0原文

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

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

谢谢,

For some reason the tooltips won't show in my child grid.

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

I keep getting the "mygrid tooltip 2" being displayed, even though I have overridden the tooltip for it's children - it won't show.

I have lessened the complexity from having control templates in resource dictionaries until I am now only left with this above, and still nothing.

any ideas will be greatly appreciated, also perhaps links where I can read up about it. my WPF book and msdn isn't producing anything substancial at the moment.

thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半山落雨半山空 2024-11-15 23:57:15

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

对于面板,这是通过将背景颜色设置为除 null(这是所有面板的默认设置)之外的颜色来完成的。如果您希望面板不可见但仍符合命中测试的条件,则可以使用透明作为背景颜色。

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

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

In order for something in WPF to be able to display a tooltip, it has to be visible to hit testing.

In the case of panels, this is done by setting the background color to something besides null (which is the default for all panels). If you want your panels to be invisible but still be eligible for hit testing, you can use Transparent as the background color.

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

<Grid ToolTip="This Will NOT Be Shown">
    <!-- other stuff -->
</Grid>
拥醉 2024-11-15 23:57:15

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

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

以前 Grid.Rows 是由 ContentControls 填充的,我用包含我需要显示的信息的本地 Stackpanels 替换了它,这似乎已经解决了这个问题,但只有在我将“Transparent”标签添加到 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>

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

@Isak, thanks, your Background="Transparent" assisted in my final solution.

I ultimately threw away the Grid with defined rows / columns, and resorted to a Grid with nested StackPanels.

Previously the Grid.Rows were being populated by ContentControls, I replaced this with local Stackpanels containing the information I needed to display and that seemed to have solved it, but only after I added the "Transparent" tag to the stackpanels and also a

IsHitTestVisible="False"

on an image in the parent grid which serves as a background image.

Herewith an example of my current solution, the second part which replaces the code seen in my original post.

First, the basic source file layout before getting to the code in question looks like this:

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

And then my solution grid as follows [replaces initial post code]:

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

Hope this helps somebody else should you experience something similar.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文