WPF:在多个控件上应用自定义样式的工具提示
我正在使用WPF应用程序.我创建了一个自定义控件库,在其中自定义了所有控件,这意味着添加了一些功能并重新设置了它们的样式.同样,我也重新设置了工具提示的样式.我正在其他项目中使用此自定义库.除ToolTip之外,其他一切都正常. ToolTip样式未得到应用.任何帮助.
I am working with WPF application. I have created a custom control library where I have customized all controls, meaning added some functionality and restyled them. Same way I have restyled the ToolTip as well. I am using this custom library in other projects. Everything is working fine except ToolTip. ToolTip style is not getting applied. Any Help plz.
我创建了一个名为ToolTip的自定义类,该类从System.Windows.Controls.ToolTip派生,我也为自定义类声明了样式.现在,我想知道如何将这种样式应用于每个控件的工具提示,这意味着我想在用户在控件上设置工具提示时创建我的工具提示的对象.
I have created a custom class named ToolTip that derives from System.Windows.Controls.ToolTip, I have declared style for my custom class as well. Now I want to know how can I get this style applied for ToolTip of each control, I mean I want to create object of my ToolTip whenever user set ToolTip on a control.
.cs中的
public class ToolTip : System.Windows.Controls.ToolTip
{
static ToolTip()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ToolTip), new FrameworkPropertyMetadata(typeof(ToolTip)));
}
}
在.xaml(资源字典)中:
in .xaml(Resource Dictionary):
<Style TargetType="{x:Type local:ToolTip}">
<Setter Property="VerticalOffset" Value="-2" />
<Setter Property="HorizontalOffset" Value="20" />
<Setter Property="Height" Value="35"></Setter>
<Setter Property="Placement" Value="Top" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:ToolTip}">
<Grid Name="Border" Background="Transparent" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}">
<Rectangle RadiusX="7.5" RadiusY="7.5">
<Rectangle.Fill>
<LinearGradientBrush StartPoint="0.5,-0.5" EndPoint="0.547,0.913">
<GradientStop Color="#FFEEEEEE" Offset="0"/>
<GradientStop Color="#FFBBBBBB" Offset="1"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<ContentPresenter Margin="10,0,10,0" HorizontalAlignment="Center" VerticalAlignment="Center" TextBlock.Foreground="Black" TextBlock.FontSize="12" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
请帮助任何人.
将默认的工具提示样式放置在单独的程序集中的共享ResourceDictionary中,将允许它被多个项目使用.您可以使用以下方法将SharedStyleLibrary.dll的Resources文件夹中的MyDefaultStyles ResourceDictionary合并到App.xaml中:
Placing the default ToolTip Style in a shared ResourceDictionary in a separate assembly will allow it to be used by multiple projects. You can merge in the MyDefaultStyles ResourceDictionary in the Resources folder of the SharedStyleLibrary.dll into App.xaml using:
<App.Resources>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/SharedStyleLibrary;component/Resources/MyDefaultStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</App.Resources>
如果MyDefaultStyles包含隐式工具提示样式(<Style TargetType="{x:Type ToolTip}">
),它将用作默认值.您还可以通过将样式指定为x:Key,然后在想要应用的任何范围(即窗口,用户控件,单个布局面板)中创建隐式工具提示样式,来更局部地定位它:
If MyDefaultStyles contains an implicit ToolTip Style (<Style TargetType="{x:Type ToolTip}">
) it will be used as the default. You can also target it more locally by instead giving the Style an x:Key and then creating an implicit ToolTip Style in whatever scope you want it to apply (i.e. Window, UserControl, single layout Panel):
<Style TargetType="{x:Type ToolTip}" BasedOn="{StaticResource ExternalLibraryNamedStyle}">...