如何将 WPF 工具提示样式设置成类似于气泡的形状?

21

我希望能够像下面的图片一样设计我的 WPF 工具提示:

enter image description here

我如何实现这个效果?


我最近遇到了类似的问题,并撰写了两篇文章,我认为可能会有所帮助:http://pmichaels.net/2016/04/01/tooltip-speech-bubbles/ 和 http://pmichaels.net/2016/04/08/creating-a-speech-bubble-with-rounded-corners/。 - Paul Michaels
2个回答

46

使用这段代码:

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing"
    x:Name="Window"
    Title="MainWindow"
    Width="640"
    Height="480">

<Window.Resources>

    <Style x:Key="{x:Type ToolTip}" TargetType="ToolTip">
        <Setter Property="OverridesDefaultStyle" Value="true" />
        <Setter Property="HasDropShadow" Value="True" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ToolTip">
                    <ed:Callout Name="Border"
                                Width="{TemplateBinding Width}"
                                Height="{TemplateBinding Height}"
                                MinWidth="100"
                                MinHeight="30"
                                Margin="0,0,0,50"
                                AnchorPoint="0,1.5"
                                Background="{StaticResource LightBrush}"
                                BorderBrush="{StaticResource SolidBorderBrush}"
                                BorderThickness="1"
                                CalloutStyle="RoundedRectangle"
                                Fill="#FFF4F4F5"
                                FontSize="14.667"
                                Stroke="Black">
                        <ContentPresenter Margin="4"
                                          HorizontalAlignment="Left"
                                          VerticalAlignment="Top" />
                    </ed:Callout>

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

</Window.Resources>
<Grid>
    <Button ToolTip="Hello" />
</Grid>

这是开头,现在你可以和它互动......享受吧!

在此输入图片描述


谢谢哈利。我还有一个问题:我应该为xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing"添加一个程序集吗? - Mohammad Zare
8
好的,添加 Microsoft.Expression.Drawing 程序集。 - Harry
这个工具提示显示在控件下面!如何将其显示在按钮上方? - SepehrM
2
只需添加以下代码即可在控件下方显示:<Setter Property="Placement" Value="Top"/> - SepehrM
我不确定为什么它不再工作了,即使新的命名空间http://schemas.microsoft.com/expression/blend/2008没有引发任何错误。 - Cfun
使用这种方法,您如何将气泡指向正确的目标并放置在正确的位置?这是自动管理的吗? - Willy

0
 <Style x:Key="BalloonTooltipStyle" TargetType="ToolTip">
        <Setter Property="OverridesDefaultStyle" Value="True" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ToolTip">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <Path x:Name="BalloonPointer" Fill="#6082B6" Stretch="Fill" Data="M0,0 L0,-1 -1,-0.5 Z" Width="15" Height="15" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="5,35,0,0" />
                        <Border Background="#6082B6"   BorderBrush="#6082B6"  Grid.Column="1">
                            <Grid>
                                <ContentPresenter Content="{TemplateBinding Content}"   />
                            </Grid>
                        </Border>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="Placement" Value="Right">
                            <Setter TargetName="BalloonPointer" Property="Margin" Value="5,0,0,0" />
                        </Trigger>
                        <Trigger Property="Placement" Value="Left">
                            <Setter TargetName="BalloonPointer" Property="Margin" Value="-5,0,0,0" />
                        </Trigger>
                        <Trigger Property="Placement" Value="Top">
                            <Setter TargetName="BalloonPointer" Property="Margin" Value="0,-5,0,0" />
                        </Trigger>
                        <Trigger Property="Placement" Value="Bottom">
                            <Setter TargetName="BalloonPointer" Property="Margin" Value="0,0,0,-5" />
                        </Trigger>
                    </ControlT[1]emplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

你的回答可以通过提供更多支持信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的答案是正确的。您可以在帮助中心中找到有关如何编写良好答案的更多信息。 - Community

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接