C# 使用模板以编程方式显示工具提示

C# 使用模板以编程方式显示工具提示,c#,wpf,tooltip,C#,Wpf,Tooltip,我想显示一个工具提示(可以使用)。但是现在我想在工具提示上应用一个模板,这样它看起来比一个矩形更好 在我的窗口的xaml文件中,我有以下资源: <Window.Resources> <LinearGradientBrush x:Key="LightBrush" StartPoint="0,0" EndPoint="0,1"> <GradientBrush.GradientStops> <GradientStopCo

我想显示一个工具提示(可以使用)。但是现在我想在工具提示上应用一个模板,这样它看起来比一个矩形更好

在我的窗口的xaml文件中,我有以下资源:

<Window.Resources>
    <LinearGradientBrush x:Key="LightBrush" StartPoint="0,0" EndPoint="0,1">
    <GradientBrush.GradientStops>
            <GradientStopCollection>
                <GradientStop Color="#FFF" Offset="0.0"/>
                <GradientStop Color="#EEE" Offset="1.0"/>
            </GradientStopCollection>
        </GradientBrush.GradientStops>
    </LinearGradientBrush>

    <SolidColorBrush x:Key="SolidBorderBrush" Color="#888" />

    <Style x:Key="{x:Type ToolTip}" TargetType="ToolTip">
        <Setter Property="OverridesDefaultStyle" Value="true" />
        <Setter Property="HorizontalOffset" Value="0" />
        <Setter Property="VerticalOffset" Value="0" />
        <Setter Property="Background" Value="GhostWhite" />
        <Setter Property="Foreground" Value="Gray" />
        <Setter Property="FontSize" Value="12" />
        <Setter Property="FontFamily" Value="Segoe UI" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ToolTip">
                    <Canvas Width="200" Height="100">
                        <Path x:Name="Container"
                  Canvas.Left="0"
                  Canvas.Top="0"
                  Margin="0"
                  Data="M 50,10 L60,0 L70,10 L100,10 L100,100 L0,100 L0,10 L50,10"
                  Fill="{TemplateBinding Background}"
                  Stroke="Black">
                            <Path.Effect>
                                <DropShadowEffect BlurRadius="10"
                                      Opacity="0.5"
                                      ShadowDepth="4" />
                            </Path.Effect>
                        </Path>
                        <TextBlock Canvas.Left="50"
                       Canvas.Top="28"
                       Width="100"
                       Height="65"
                       Text="{TemplateBinding Content}"
                       TextWrapping="Wrapwithoverflow" />
                    </Canvas>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
这使得工具提示在3秒后再次消失。 遗憾的是,模板未应用于,工具提示仅显示文本

有人能帮我吗


谢谢大家!

您应该为工具提示样式指定一个键名,例如
TooltipStyleKey
,并在代码隐藏中设置它

public static void ShowError(object sender, string text)
{
    Style style = (Style)this.FindResource("TooltipStyleKey");
    ToolTip errormsg = new ToolTip { Content = text, Style = style };
    ToolTipService.SetShowDuration(errormsg, 3000);

    if (!errormsg.IsOpen)
    {
        errormsg.Opened += async delegate (object o, RoutedEventArgs args)
        {
            var s = o as ToolTip;
            // let the tooltip display for 2 second
            await Task.Delay(1000);
            s.IsOpen = false;

            ((FrameworkElement)sender).ToolTip = null;
        };
        errormsg.IsOpen = true;
     }
}
但是,最好使用XAML显示工具提示。您可以在viewmodel中拥有错误消息的属性,并将其绑定到工具提示的内容

public static void ShowError(object sender, string text)
{
    Style style = (Style)this.FindResource("TooltipStyleKey");
    ToolTip errormsg = new ToolTip { Content = text, Style = style };
    ToolTipService.SetShowDuration(errormsg, 3000);

    if (!errormsg.IsOpen)
    {
        errormsg.Opened += async delegate (object o, RoutedEventArgs args)
        {
            var s = o as ToolTip;
            // let the tooltip display for 2 second
            await Task.Delay(1000);
            s.IsOpen = false;

            ((FrameworkElement)sender).ToolTip = null;
        };
        errormsg.IsOpen = true;
     }
}