工具提示未在WPF标签中显示?

时间:2020-12-13 07:27:56

In application, I am trying to set a Tool-tip on Label control. Please find the detail XAML, suggest to fix the issue if possible. So far i have tried many tricks (e.g IsEnabled, IsHitTestVisible, control template, background setting...), but failed to display tooltip.

在应用程序中,我正在尝试在Label控件上设置工具提示。请找到详细的XAML,建议尽可能解决问题。到目前为止,我已经尝试了许多技巧(例如IsEnabled,IsHitTestVisible,控制模板,背景设置......),但未能显示工具提示。

<UserControl x:Class="HSDLAdminPortal.Forms.Application.ApplicationDetailForm"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Layout.Toolkit"             
         xmlns:local="clr-namespace:HSDLAdminPortal.Forms.Application"
         mc:Ignorable="d" Width="1290" Height="595"
         xmlns:gif="http://wpfanimatedgif.codeplex.com"              
         xmlns:name="UserControl"
         >
<UserControl.Resources>
    <Style x:Key="LabelInfoStyle" TargetType="{x:Type Label}">
        <Setter Property="FontSize" Value="12"/>
        <Setter Property="Foreground" Value="Black"/>
    </Style>
    <Style x:Key="LabelInfoValueStyle" TargetType="{x:Type Label}">
        <Setter Property="FontSize" Value="12"/>
        <Setter Property="Foreground" Value="#999966"/>
    </Style>
    <Style x:Key="LabelInfoHeaderStyle" TargetType="{x:Type Label}">
        <Setter Property="FontSize" Value="14"/>
        <Setter Property="Foreground" Value="White"/>
    </Style>
</UserControl.Resources>
<Grid Margin="4" Background="Transparent">
    <Grid.Background>
        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="#e6eeff" Offset="1" />
            <GradientStop Color="#FFF5F2EF" Offset="0" />
        </LinearGradientBrush>
    </Grid.Background>
    <Grid.Resources>
        <ResourceDictionary Source="/Resources/CustomAccordion.xaml"/>
    </Grid.Resources>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="auto" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="auto" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Grid Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Margin="0,-10,730,0" Background="Transparent" Panel.ZIndex="1">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="auto" />
        </Grid.RowDefinitions>
        <StackPanel>
            <GroupBox Header=""  Margin="0,4,10,4" Height="540" HorizontalAlignment="Right" Width="532">
                <Canvas Margin="0,0,0,1">
                    <Rectangle HorizontalAlignment="Left" Height="19" Stroke="White" VerticalAlignment="Top" Width="493" Canvas.Left="17" Canvas.Top="201">
                        <Rectangle.Fill>
                            <LinearGradientBrush  EndPoint="0.5,1" StartPoint="0.5,0">
                                <GradientStop Color="LightGray" Offset="1" />
                                <GradientStop Color="LightGray" />
                                <GradientStop Color="White" Offset="0.5" />
                            </LinearGradientBrush>
                        </Rectangle.Fill>
                    </Rectangle>
                    <Rectangle Fill="Transparent" HorizontalAlignment="Left" Height="295" Stroke="Gray" VerticalAlignment="Top" Width="493" Canvas.Left="17" Canvas.Top="219"/>
                    <Image Stretch="Fill"  x:Name="ImagePhoto" HorizontalAlignment="Left" VerticalAlignment="Top" Height="123"  Width="120" Source="/HSDLAdminPortal;component/Resources/photo_test.png" Canvas.Top="3" Canvas.Left="37"/>
                    <Label x:Name="LblPhoto" Content="Photo" HorizontalAlignment="Left" Background="Transparent"  VerticalAlignment="Top" FontSize="10" FontWeight="Bold" Height="22" Width="62" Canvas.Top="124" Canvas.Left="75"/>
                    <Border BorderBrush="Black" BorderThickness="1" Height="128" Canvas.Left="37" Canvas.Top="1" Width="119"/>
                    <Rectangle Height="22" Canvas.Left="221"  Canvas.Top="1" Width="289">
                        <Rectangle.Fill>
                            <LinearGradientBrush  EndPoint="0.5,1" StartPoint="0.5,0">
                                <GradientStop Color="LightGray" Offset="1" />
                                <GradientStop Color="LightGray" />
                                <GradientStop Color="White" Offset="0.5" />
                            </LinearGradientBrush>
                        </Rectangle.Fill>
                    </Rectangle>
                    <Rectangle Fill="Transparent" HorizontalAlignment="Left" Height="163" Stroke="White" VerticalAlignment="Top" Width="289" Canvas.Left="221" Canvas.Top="23"/>
                    <Label x:Name="LblMiscellaneous" Style="{DynamicResource LabelInfoHeaderStyle}" Foreground="Black" Content="Miscellaneous" Background="Transparent" Canvas.Left="342" Canvas.Top="-1" Height="28" FontWeight="ExtraBold"/>
                    <Label x:Name="LblPersonalInfo" Style="{DynamicResource LabelInfoHeaderStyle}" Content="Personal Info" Foreground="Black" Background="Transparent" Canvas.Left="221" Canvas.Top="195" Height="54" Width="125" FontWeight="ExtraBold"/>

                    <Label x:Name="LblApplicantType" ToolTip="text tooltip" Style="{DynamicResource LabelInfoStyle}" Content="Applicant Type : " Background="Transparent" Canvas.Left="231" Canvas.Top="40" FontWeight="Bold"/>
                    <Label x:Name="LblApplicantTypeValue" Style="{DynamicResource LabelInfoValueStyle}" Content="N/A" Background="Transparent" Canvas.Left="360" Canvas.Top="40"  FontWeight="Bold"/>

                    <Label x:Name="LblApplicantNameValue" Style="{DynamicResource LabelInfoValueStyle}" ToolTip="Sample ToolTip" Content="N/A" ToolTip="Sample ToolTip" Background="Transparent" Canvas.Left="190" Canvas.Top="220"  FontWeight="Bold"/>
                    <Image x:Name="ImageSignature" HorizontalAlignment="Left" VerticalAlignment="Top" Height="37"  Width="97" Source="/HSDLAdminPortal;component/Resources/signature.png" Canvas.Top="149" Canvas.Left="52"/>
                    <Border BorderBrush="Black" BorderThickness="1" Height="44" Canvas.Left="37" Canvas.Top="142" Width="119"/>
                    <Label x:Name="LblSignature" Content="Signature" HorizontalAlignment="Left" Background="Transparent"  VerticalAlignment="Top" FontSize="10" FontWeight="Bold" Height="36" Width="62" Canvas.Top="181" Canvas.Left="69"/>
                    <Canvas x:Name="AttachmentDetailsCanvas" Width="520" Height="501"  Visibility="Visible" Opacity="0">
                        <Canvas.Background>
                            <LinearGradientBrush  EndPoint="0.5,1" StartPoint="0.5,0">
                                <GradientStop Color="LightGray" Offset="1" />
                                <GradientStop Color="LightGray" />
                                <GradientStop Color="White" Offset="0.5" />
                            </LinearGradientBrush>

                        </Canvas.Background>

                        <Rectangle Height="45" Canvas.Left="0"  Canvas.Top="1" Width="520">
                            <Rectangle.Fill>
                                <LinearGradientBrush  EndPoint="0.5,1" StartPoint="0.5,0">
                                    <GradientStop Color="LightGray" Offset="1.2" />
                                    <GradientStop Color="LightGray" />
                                    <GradientStop Color="#0077b3" Offset="0" />
                                </LinearGradientBrush>
                            </Rectangle.Fill>
                        </Rectangle>
                        <Label x:Name="attachmentDetails" Content="Attachment Details" Foreground="White"  Background="Transparent" FontSize="20"  VerticalAlignment="Top"  FontWeight="Bold" Height="36" Width="220" Canvas.Top="4" Canvas.Left="143" />

                        <Image Source="/Resources/Images/attachment.png" Height="400" Width="510" Canvas.Top="86" x:Name="atcImg"/>
                        <Button ToolTip="Click To Hide Image" x:Name="hideImageButtonButton" Width="42" Height="42" Click="hideImageButton_Click" Canvas.Left="478" Canvas.Top="1">
                            <StackPanel Orientation="Horizontal">
                                <Image x:Name="close_image" HorizontalAlignment="Center" VerticalAlignment="Center" Source="/Resources/hide.png" Height="42" Width="42" OpacityMask="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" Stretch="UniformToFill" />
                            </StackPanel>
                        </Button>
                    </Canvas>
                </Canvas>
            </GroupBox>
        </StackPanel>
    </Grid>

</Grid>

工具提示未在WPF标签中显示?

3 个解决方案

#1


3  

Explicit style. The Style property is set directly. In most scenarios, the style is not defined inline, but instead is referenced as a resource, by explicit key. In this case the Style property itself acts as if it were a local value, precedence item 3.

明确的风格。 Style属性是直接设置的。在大多数情况下,样式不是内联定义的,而是通过显式键引用为资源。在这种情况下,Style属性本身就像是本地值,优先级项3。

MSDN website on precedence.

MSDN网站优先。

As the Style is set as a local value it takes precedence over the next local value.

由于样式设置为本地值,因此它优先于下一个本地值。

Add Tooltip to your style and add the text you want there for it to work with your dynamic resource

将工具提示添加到您的样式并添加您想要的文本,以便它可以使用您的动态资源

UPDATE

After looking at your code, it is clear that your label is just covered by a transparent grid, so your mouseover never really goes over it. Put, Panel.ZIndex="1" and it will work as this brings your label in front of everything

在查看您的代码之后,很明显您的标签只是由透明网格覆盖,因此您的鼠标悬停从未真正覆盖它。 Put,Panel.ZIndex =“1”,它将起作用,因为这会将您的标签带到一切的前面

I would also look at the MSDN website, if you plan on using more than one zindex to overcome your problem, just to learn more about it.

如果您计划使用多个zindex来解决您的问题,我也会查看MSDN网站,只是为了了解更多信息。

https://msdn.microsoft.com/en-us/library/system.windows.controls.panel.zindex(v=vs.110).aspx

  <Label x:Name="LblApplicantNameValue" Panel.ZIndex="1" Style="{DynamicResource LabelInfoValueStyle}" ToolTip="Sample ToolTip" Content="this label now works"  Background="Transparent" Canvas.Left="360" Canvas.Top="66"  FontWeight="Bold"/>

#2


0  

You can style the tool tip by overriding the template of ToolTip control as did in the question How to style WPF tooltip like a speech bubble?

您可以通过覆盖ToolTip控件的模板来设置工具提示的样式,如问题如何像讲话泡泡样式WPF工具提示一样?

#3


0  

The position of following image (attachment.png) was placed at the same position of labels, but not showing because of 0 opacity (at parent control). Now i use Visibility of image instead of Opacity & it fix my issue. thank you all.

跟随图像(attachment.png)的位置被放置在标签的相同位置,但由于0不透明度(在父控件处)而未显示。现在我使用图像的可见性而不是不透明度它解决了我的问题。谢谢你们。

#1


3  

Explicit style. The Style property is set directly. In most scenarios, the style is not defined inline, but instead is referenced as a resource, by explicit key. In this case the Style property itself acts as if it were a local value, precedence item 3.

明确的风格。 Style属性是直接设置的。在大多数情况下,样式不是内联定义的,而是通过显式键引用为资源。在这种情况下,Style属性本身就像是本地值,优先级项3。

MSDN website on precedence.

MSDN网站优先。

As the Style is set as a local value it takes precedence over the next local value.

由于样式设置为本地值,因此它优先于下一个本地值。

Add Tooltip to your style and add the text you want there for it to work with your dynamic resource

将工具提示添加到您的样式并添加您想要的文本,以便它可以使用您的动态资源

UPDATE

After looking at your code, it is clear that your label is just covered by a transparent grid, so your mouseover never really goes over it. Put, Panel.ZIndex="1" and it will work as this brings your label in front of everything

在查看您的代码之后,很明显您的标签只是由透明网格覆盖,因此您的鼠标悬停从未真正覆盖它。 Put,Panel.ZIndex =“1”,它将起作用,因为这会将您的标签带到一切的前面

I would also look at the MSDN website, if you plan on using more than one zindex to overcome your problem, just to learn more about it.

如果您计划使用多个zindex来解决您的问题,我也会查看MSDN网站,只是为了了解更多信息。

https://msdn.microsoft.com/en-us/library/system.windows.controls.panel.zindex(v=vs.110).aspx

  <Label x:Name="LblApplicantNameValue" Panel.ZIndex="1" Style="{DynamicResource LabelInfoValueStyle}" ToolTip="Sample ToolTip" Content="this label now works"  Background="Transparent" Canvas.Left="360" Canvas.Top="66"  FontWeight="Bold"/>

#2


0  

You can style the tool tip by overriding the template of ToolTip control as did in the question How to style WPF tooltip like a speech bubble?

您可以通过覆盖ToolTip控件的模板来设置工具提示的样式,如问题如何像讲话泡泡样式WPF工具提示一样?

#3


0  

The position of following image (attachment.png) was placed at the same position of labels, but not showing because of 0 opacity (at parent control). Now i use Visibility of image instead of Opacity & it fix my issue. thank you all.

跟随图像(attachment.png)的位置被放置在标签的相同位置,但由于0不透明度(在父控件处)而未显示。现在我使用图像的可见性而不是不透明度它解决了我的问题。谢谢你们。