WPF-使用样式触发器设置自定义工具提示 [英] WPF - Setting Custom tooltip using style triggers
本文介绍了WPF-使用样式触发器设置自定义工具提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试基于属性HasValidationError将工具提示显示在堆栈面板上。
I am trying to display tool tip to a stack panel based on property HasValidationError.
<Style TargetType="StackPanel" x:Key="stackstyle">
<Style.Triggers>
<DataTrigger Binding="{Binding HasValidationError}" Value="True">
<Setter Property="ToolTip">
<Setter.Value>
<Binding Path="DisplayError"/>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
代码工作正常。但它会在黄色背景下显示工具提示(作为普通工具提示)。我需要对其进行自定义以更改并包含图像。为此,
The code works fine. But it displays the tooltip under yellow background ( as normal tooltip). I need to customize it to change and include image. For that,
<Style TargetType="StackPanel" x:Key="stackstyle">
<Style.Triggers>
<DataTrigger Binding="{Binding HasValidationError}" Value="True">
<Setter Property="ToolTip">
<Setter.Value>
<StackPanel>
<!-- Have to add image and other decorations here -->
<TextBlock Text = "{Binding DisplayError}"/>
</StackPanel>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
在将StackPanel添加到时显示错误。请帮助我解决问题。
It shows error when adding StackPanel to the . Please help me in solving.
推荐答案
我不知道为什么会失败,但是您可以通过制作工具提示来解决此问题。资源:
I don't know why that fails, but you can work around it by making the ToolTip a resource:
<StackPanel x:Key="ToolTipContents">
<!-- Have to add image and other decorations here -->
<TextBlock Text = "{Binding DisplayError}"/>
</StackPanel>
<Style TargetType="StackPanel" x:Key="stackstyle">
<Style.Triggers>
<DataTrigger Binding="{Binding HasValidationError}" Value="True">
<Setter Property="ToolTip" Value="{StaticResource ToolTipContents}"/>
</DataTrigger>
</Style.Triggers>
</Style>
或
<ToolTip x:Key="ToolTipContents">
<StackPanel>
<!-- Have to add image and other decorations here -->
<TextBlock Text = "{Binding DisplayError}"/>
</StackPanel>
</ToolTip>
<!-- etc -->
此外,您拥有的代码将按.NET 4编写,因此该错误已得到修复。 。
Also, the code you have will work as written in .NET 4, so the bug has been fixed.
这篇关于WPF-使用样式触发器设置自定义工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文