我想创建一个WPF工具提示,其中包含工具提示标题的标签,然后是包含更详细文本的文本块.我在资源字典中创建了以下样式:
<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>
Run Code Online (Sandbox Code Playgroud)
并且可以将此样式成功应用于这样的按钮并显示工具提示标题:
<Button.ToolTip>
<ToolTip Style="{DynamicResource PalletToolTip}">
<Binding Source="{x:Static ResStrings.New}"/>
</ToolTip>
</Button.ToolTip>
Run Code Online (Sandbox Code Playgroud)
我坚持的是如何从上面的用法中设置额外描述性文本的内容?在显示工具提示标题时,我已经将数据绑定到Content属性.任何读过Adam Nathan的WPF Unleashed书的人都会认识到我正在使用他的示例工具提示XAML,但在他的情况下,他使用硬编码字符串作为标签和文本块的内容.我想创建一些更可重用的东西,因此希望使用数据绑定来实现相同的效果.