WPF 自定义工具提示
WPF Custom ToolTip
我正在使用 .Net 4.7.2 和 WPF 4.5.1
我创建了一个用作工具提示的自定义控件。自定义控件有效,我可以将其应用于 UI 控件,例如 TextBox。
不幸的是,我没有找到摆脱常用工具提示的典型框架的方法,这让我想起了一个按钮。
不过看看
ToolTip with a frame
我尝试覆盖工具提示的默认样式,如 Whosebug 和其他网站上的几个示例所示。
不幸的是,这并没有解决我的问题。
我的 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>
自定义控件不是从工具提示派生的,而是从控件派生的
public class ToolTip : Control
{
// ...
}
正如我已经提到的,控件代码没有问题,我正在努力解决样式问题。
边框来自 ToolTip
的 ControlTemplate
。更改 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>
所以它看起来没有和有 Style
:
我正在使用 .Net 4.7.2 和 WPF 4.5.1
我创建了一个用作工具提示的自定义控件。自定义控件有效,我可以将其应用于 UI 控件,例如 TextBox。
不幸的是,我没有找到摆脱常用工具提示的典型框架的方法,这让我想起了一个按钮。
不过看看
ToolTip with a frame
我尝试覆盖工具提示的默认样式,如 Whosebug 和其他网站上的几个示例所示。
不幸的是,这并没有解决我的问题。
我的 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>
自定义控件不是从工具提示派生的,而是从控件派生的
public class ToolTip : Control
{
// ...
}
正如我已经提到的,控件代码没有问题,我正在努力解决样式问题。
边框来自 ToolTip
的 ControlTemplate
。更改 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>
所以它看起来没有和有 Style
: