工具提示放置 - WPF

时间:2013-07-03 13:42:45

标签: wpf

我正在尝试为一个简单的按钮组合一个工具提示。但是,当鼠标悬停在按钮上时,工具提示不会出现在其下方。

请参阅:enter image description here

此xaml如下:

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Height="300" Width="300"  xmlns:sys="clr-namespace:System;assembly=mscorlib">

    <Page.Resources>

        <Style x:Key="ToolTipStyle" TargetType="{x:Type ToolTip}">
            <Setter Property="OverridesDefaultStyle" Value="true" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ToolTip">
                        <Grid x:Name="PopupGrid">
                            <Grid x:Name="ShadowBackground" Height="65" Width="260">
                                <Grid.Effect>
                                    <DropShadowEffect BlurRadius="7" ShadowDepth="1" Opacity="0.5" />
                                </Grid.Effect>
                                <Path Margin="0 0 50 0" Width="20" Height="10" HorizontalAlignment="Right" VerticalAlignment="Top" Data="M0,10 L10,0 20,10Z" Stroke="Gray" Fill="#EFEFF0" Stretch="None" />
                                <Border BorderThickness="1 0 1 1" CornerRadius="3" Margin="10 9 10 10" BorderBrush="Gray" Background="#EFEFF0">
                                    <ContentPresenter/>
                                </Border>
                                <Border BorderThickness="0 1 0 0" CornerRadius="0 0 3 0" Margin="0 9 10 0" HorizontalAlignment="Right" VerticalAlignment="Top" Width="41" Height="10" BorderBrush="Gray" />
                                <Border BorderThickness="0 1 0 0" CornerRadius="3 0 0 0" Margin="10 9 69 0" VerticalAlignment="Top" Height="10" BorderBrush="Gray" />

                            </Grid>                     
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="ToolTipHeaderStyle" TargetType="{x:Type Label}">
            <Setter Property="FontFamily" Value="Calibri"/>
            <Setter Property="FontWeight" Value="Bold"/>
            <Setter Property="FontSize" Value="14"/>
        </Style>

        <Style x:Key="ToolTipTextStyle" TargetType="{x:Type Label}">
            <Setter Property="FontFamily" Value="Calibri"/>
            <Setter Property="FontSize" Value="12"/>
        </Style>

    </Page.Resources>

    <Grid x:Name="PopupGrid" Background="Red">
        <Button Width="100" Height="30" Content="Click Me!">
            <Button.ToolTip>
                <ToolTip Style="{StaticResource ToolTipStyle}">
                    <StackPanel Orientation="Vertical">
                        <Label Content="Newly Rejected" Style="{StaticResource ToolTipHeaderStyle}"></Label>
                        <Label Content="Please perform requested edits and resubmit" Style="{StaticResource ToolTipTextStyle}"></Label>
                    </StackPanel>
                </ToolTip>
            </Button.ToolTip>
        </Button>
    </Grid>

</Page>

我不确定导致此行为的原因。你能帮忙让位置正确吗?

忘记提及它应该如何出现:

工具提示的三角形应该位于鼠标光标的正下方,这意味着工具提示应该向左移动。这样的事情:enter image description here

谢谢, -Mike

2 个答案:

答案 0 :(得分:5)

您是否使用了展示位置属性?

您可以将其添加到ToolTipStyle:

<Setter Property="ToolTipService.Placement" Value="Left" />

还有ToolTipService.PlacementRectangle和ToolTipService.PlacementTarget

编辑

你可以尝试:

<Setter Property="ToolTipService.HorizontalOffset" Value="-200" />

答案 1 :(得分:3)

使用CustomPopupPlacementCallback ..在我的情况下,我需要在文本框右侧显示提示,Placement.Right在我的笔记本电脑上正常工作,但在触摸屏左侧显示,最简单的解决方法是使用回调来计算后面代码中的相对偏移量:

...
tip.PlacementTarget = this;
tip.Placement = PlacementMode.Custom;
tip.CustomPopupPlacementCallback = new CustomPopupPlacementCallback(PositionTooltip);
...

    private CustomPopupPlacement[] PositionTooltip(Size popupSize, Size targetSize, Point offset)
    {
        double offsetY = targetSize.Height / 2 + popupSize.Height;
        double offsetX = targetSize.Width;

        return new CustomPopupPlacement[] { new CustomPopupPlacement(new Point(offsetX, offsetY), PopupPrimaryAxis.None) };
    }