WPF自定义工具提示

时间:2019-01-07 08:22:13

标签: wpf xaml tooltip wpf-style

我正在使用.Net 4.7.2和WPF 4.5.1

我创建了一个自定义控件,用作ToolTip。自定义控件有效,我可以将其应用于UI控件,例如TextBox。

不幸的是,我找不到一种方法来摆脱通用工具提示的典型框架,这使我想起了Button。

但是看看

ToolTip with a frame

我尝试覆盖默认的工具提示样式,如在stackoverflow和其他站点上的几个示例中所示。

不幸的是,这不能解决我的问题。

我的xaml代码如下:

<TextBox>
    <TextBox.ToolTip>
        <local:MyToolTip Text="{Binding MyToolTipText}" />
    </TextBox.ToolTip>
</TextBox>

如何删除工具提示周围的边框之类的按钮?

预先感谢

说明:

我尝试了两种方法:

第一个:

<Style TargetType="{x:Type ToolTip}">

    <Setter Property="SnapsToDevicePixels"                        Value="True" />

    <Setter Property="Control.Template">
        <Setter.Value>
            <ControlTemplate>

                <Border VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Background="Black">

                    <!-- other content -->

                </Border>


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

</Style>

第二个:

<Style TargetType="{x:Type MyToolTip}">

    <Setter Property="SnapsToDevicePixels"                       Value="True" />

    <Setter Property="Control.Template">
        <Setter.Value>
            <ControlTemplate>

                <local:MyToolTip />

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

</Style>

自定义控件不是从ToolTip派生的,而是从Control派生的

public class ToolTip : Control
{
    // ...
}

正如我已经提到的,控件的代码没有问题,我正在努力解决样式问题。

1 个答案:

答案 0 :(得分:0)

边界来自ControlTemplate的{​​{1}}。更改ToolTip,您将摆脱边界:

ControlTemplate

因此,在没有<TextBox Text="text box text"> <TextBox.Resources> <Style TargetType="ToolTip"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ToolTip"> <ContentPresenter /> </ControlTemplate> </Setter.Value> </Setter> </Style> </TextBox.Resources> <TextBox.ToolTip> <TextBlock Text="TEST" > </TextBlock> </TextBox.ToolTip> </TextBox> 的情况下看起来很明显:
enter image description here