我想创建一个 WPF 工具提示,其中包含工具提示标题的标签,然后创建一个包含更详细文本的文本 block 。我在资源字典中创建了以下样式:
<Style x:Key="AppToolTip"
TargetType="ToolTip">
<Setter Property="OverridesDefaultStyle" Value="true" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToolTip">
<StackPanel>
<Label Content="{TemplateBinding Content}" FontWeight="Bold" Background="Blue" Foreground="White">
</Label>
<TextBlock Padding="10" TextWrapping="WrapWithOverflow" Width="200">
</TextBlock>
</StackPanel>
</ControlTemplate>
</Setter.Value></Setter>
</Style>
并且可以成功地将这种样式应用于按钮,并显示工具提示标题:
<Button.ToolTip>
<ToolTip Style="{DynamicResource PalletToolTip}">
<Binding Source="{x:Static ResStrings.New}"/>
</ToolTip>
</Button.ToolTip>
我坚持的是如何从上面的用法中设置额外描述性文本的内容?显示工具提示 header 时,我已经将数据绑定(bind)到 Content 属性。
任何读过 Adam Nathan 的 WPF Unleashed 书的人都会认识到我正在使用他的示例工具提示 XAML,但在他的例子中,他使用硬编码字符串作为标签和文本 block 的内容。我想创建一些更可重用的东西,因此想使用数据绑定(bind)来实现相同的效果。
最佳答案
我会继承 HeaderedToolTip
来自 ToolTip
的类(class)并添加 Header
属性(property)。我会像您所做的那样为该控件指定模板。然后我就可以像这样使用它:
<Button>
<Button.ToolTip>
<HeaderedToolTip Header="My Title" Content="My Content"/>
</Button.ToolTip>
</Button>
或者,使用绑定(bind):
<Button>
<Button.ToolTip>
<HeaderedToolTip Header="{Binding ToolTipTitle}" Content="{Binding ToolTipText}"/>
</Button.ToolTip>
</Button>
关于wpf - 自定义 WPF 工具提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6582675/