我想设置自定义工具提示样式,但仅适用于某些类型的控件。我有一个自定义控件,用于设置颜色的工具提示取决于绑定的消息类型。
如果为"工具提示"设置样式,则所有工具提示都显示没有边框,但我只想设置我的自定义控件。
<Style x:Key="{x:Type ToolTip}" TargetType="ToolTip">
<Setter Property="OverridesDefaultStyle" Value="true" />
<Setter Property="HasDropShadow" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToolTip">
<ContentPresenter />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
这是我想要的外观&amp;感觉 http://i.imgur.com/fG1aCso.png
&#34;正常的工具提示&#34;必须在鼠标悬停按钮时显示&#34; info tooltip&#34;在蓝色文本等自定义控件上
FTI:我尝试将样式放在DLL上(定义自定义控件的位置),但不起作用。我在&#34; assembly.info&#34;上有下一个代码,所以它采用了&#34; Themes / Generic.xaml&#34;
的模板[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]
答案 0 :(得分:2)
按照您当前的方法,您可以尝试使用某些DataTrigger
收听PlacementTarget
来检查其类型并相应地设置Template
。所以我们需要一个转换器(将PlacementTarget
转换为它的类型)。这是工作代码:
<Style TargetType="ToolTip">
<Style.Resources>
<local:ElementToTypeConverter x:Key="typeConverter"/>
</Style.Resources>
<Style.Triggers>
<DataTrigger Binding="{Binding PlacementTarget,
RelativeSource={RelativeSource Self},
Converter={StaticResource typeConverter}}"
Value="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToolTip">
<Border BorderBrush="Red" BorderThickness="2">
<ContentPresenter/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding PlacementTarget,
RelativeSource={RelativeSource Self},
Converter={StaticResource typeConverter}}"
Value="{x:Type ToggleButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToolTip">
<Border BorderBrush="Blue" BorderThickness="1">
<ContentPresenter/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
<强> 代码隐藏 强>:
public class ElementToTypeConverter : IValueConverter
{
object IValueConverter.Convert(object value, Type targetType,
object parameter, System.Globalization.CultureInfo culture)
{
return value == null ? Type.Missing : value.GetType();
}
object IValueConverter.ConvertBack(object value, Type targetType,
object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
以上只是应用于2种控件类型的示例:Button
和ToggleButton
,您可以使用自己的控件类型替换它们,并将ToolTip的模板更改为您想要的任何内容。