2013-07-03 61 views
5

我正在嘗試爲一個簡單的按鈕提供一個工具提示。但是,當鼠標懸停在按鈕上時,工具提示不會顯示在其下方。Tooltip Placement - 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> 

我不知道是什麼原因造成這種現象。您能否幫助確定展示位置?

忘記提到它應該如何顯示:

工具提示的三角應該是正下方鼠標光標的地方,這將意味着提示應努力left.Something移動這樣的:enter image description here

謝謝, -Mike

+0

你可以看到這一點:http://stackoverflow.com/questions/14275755/wpf-button-with-image-trigger-mouseover-to-change -image IsMouseOver屬性未針對您的情況設置。 – Naresh

+0

我不確定鼠標將如何解決這個問題。 – Mike

+0

好吧,我從來沒有使用過工具提示,但我的理解是,當你將鼠標放在按鈕上時,它應該顯示一個框。爲此,你不需要觸發mouseover事件?我不確定。希望有些專家會澄清。 – Naresh

回答

3

您是否玩過配置屬性?

您可以添加到您的ToolTipStyle:

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

還有ToolTipService.PlacementRectangle和ToolTipService.PlacementTarget

編輯

你可以嘗試:

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

那也不管用,目標也是。 – Mike

+0

我編輯我的答案與另一種可能的方式來解決您的問題。 – TrueEddie

+0

但是,當在數據模板中指定工具提示時不起作用。參見:http://stackoverflow.com/questions/17451723/placement-target-binding-inside-a-datatemplate-tooltip – Mike

2

使用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) }; 
    }