WPF工具提示箭头放置

时间:2017-01-12 23:27:41

标签: c# .net wpf xaml tooltip

我需要一点指导。我正在尝试用箭头创建自己的工具提示。我遇到了一个问题。如果ToolTip太靠近屏幕边缘,那么当然会改变工具提示位置,但不再为自己设置箭头。

我试图通过此https://stackoverflow.com/questions/18497029/来解决它,但没有结果。

这是我的源代码。

 <Style x:Key="ToolTipArrowStyle" TargetType="{x:Type ToolTip}">
        <Setter Property="OverridesDefaultStyle" Value="true" />
        <Setter Property="HasDropShadow" Value="True" />
        <Setter Property="ToolTipService.Placement" Value="Bottom" />
        <Setter Property="ToolTipService.ShowDuration" Value="1000"/>
        <Setter Property="ToolTipService.InitialShowDelay" Value="7000"/>
        <Setter Property="ToolTipService.BetweenShowDelay" Value="2000"/>
        <Setter Property="Template">
            <Setter.Value>

                <ControlTemplate  TargetType="{x:Type ToolTip}">
                    <Grid x:Name="LayoutRoot" d:DesignWidth="260" d:DesignHeight="71" Height="Auto" Width="260">
                        <StackPanel>

                            <Path x:Name="ArrowPathTop" 
                                  Margin="0" 
                                  Width="20" Height="10" 
                                  HorizontalAlignment="Center" 
                                  VerticalAlignment="Top" 
                                  Data="M0,10 L10,0 20,10Z" 
                                  Stroke="Black" 
                                  Fill="White" 
                                  Stretch="None" />


                            <Border Height="50" Background="White" Margin="5,0" BorderBrush="Black" BorderThickness="1">
                                <Border.Effect>
                                    <DropShadowEffect BlurRadius="7" ShadowDepth="1" Opacity="0.5" />
                                </Border.Effect>
                                <ContentPresenter/>
                            </Border>

                            <Path x:Name="ArrowPathBottom" 
                                  Visibility="Hidden" 
                                  Margin="0" 
                                  Width="20" Height="10" 
                                  HorizontalAlignment="Center"
                                  VerticalAlignment="Top" 
                                  Data="M0,10 L10,0 20,10Z" 
                                  Stroke="Black" 
                                  Fill="White" 
                                  Stretch="None" 
                                  RenderTransformOrigin="0.5,0.5" >
                                <Path.RenderTransform>
                                    <TransformGroup>
                                        <ScaleTransform/>
                                        <SkewTransform/>
                                        <RotateTransform Angle="180"/>
                                        <TranslateTransform/>
                                    </TransformGroup>
                                </Path.RenderTransform>
                            </Path>
                        </StackPanel>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="Placement" Value="Top">
                            <Setter TargetName="ArrowPathBottom" Property="Visibility" Value="Visible"/>
                            <Setter TargetName="ArrowPathTop" Property="Visibility" Value="Hidden"/>
                        </Trigger>
                        <Trigger Property="Placement" Value="Bottom">
                            <Setter TargetName="ArrowPathBottom" Property="Visibility" Value="Hidden"/>
                            <Setter TargetName="ArrowPathTop" Property="Visibility" Value="Visible"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>

    </Style>
  <Button Height="40"  Style="{StaticResource TransparentButton2}"  
            Content="Button transparent" VerticalAlignment="Center" HorizontalAlignment="Center" >
        <Button.ToolTip>
            <ToolTip Style="{DynamicResource ToolTipArrowStyle}">
                <ToolTip.HorizontalOffset>
                    <MultiBinding Converter="{StaticResource CenterToolTipConverter}">
                        <Binding RelativeSource="{RelativeSource Self}" Path="PlacementTarget.ActualWidth"/>
                        <Binding RelativeSource="{RelativeSource Self}" Path="ActualWidth"/>
                    </MultiBinding>
                </ToolTip.HorizontalOffset>

                <StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">
                    <TextBlock Text="ToolTip text" FontSize="15" Foreground="Red"/>
                </StackPanel>
            </ToolTip>
        </Button.ToolTip>
    </Button>

有谁知道如何解决这个问题?非常感谢。

见图:

enter image description here enter image description here

0 个答案:

没有答案