简体   繁体   English

WPF:在多个控件中应用自定义样式的工具提示

[英]Wpf: Apply custom style of ToolTip at multiple controls

I am working with WPF application. 我正在使用WPF应用程序。 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之外,其他一切都正常。 ToolTip style is not getting applied. 工具提示样式未得到应用。 Any Help plz. 任何帮助。

Edit: 编辑:

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. 我创建了一个名为ToolTip的自定义类,该类从System.Windows.Controls.ToolTip派生,我也为自定义类声明了样式。 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. 现在,我想知道如何将这种样式应用于每个控件的工具提示,这意味着我想在用户在控件上设置工具提示时创建我的工具提示的对象。

in .cs: 在.cs中:

public class ToolTip : System.Windows.Controls.ToolTip
{
    static ToolTip()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(ToolTip), new FrameworkPropertyMetadata(typeof(ToolTip)));
    }
}

in .xaml(Resource Dictionary): 在.xaml(资源字典)中:

<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>

Any Help plz. 任何帮助。

If your custom ToolTip class is only for the sake of applying templates, it's much easier to use an Implicit Style for your ToolTips: 如果您的自定义ToolTip类仅是为了应用模板,则对工具提示使用隐式样式会容易得多:

<Style TargetType="{x:Type ToolTip}">
    <Setter Property="OverridesDefaultStyle" Value="True" />
    <!-- rest of your style here -->
</Style>

Placing the default ToolTip Style in a shared ResourceDictionary in a separate assembly will allow it to be used by multiple projects. 将默认的工具提示样式放在单独的程序集中的共享ResourceDictionary中,将允许多个项目使用它。 You can merge in the MyDefaultStyles ResourceDictionary in the Resources folder of the SharedStyleLibrary.dll into App.xaml using: 您可以使用以下命令将SharedStyleLibrary.dll的Resources文件夹中的MyDefaultStyles ResourceDictionary合并到App.xaml中:

  <App.Resources>
    <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="/SharedStyleLibrary;component/Resources/MyDefaultStyles.xaml"/>
    </ResourceDictionary.MergedDictionaries>
  </App.Resources>

If MyDefaultStyles contains an implicit ToolTip Style ( <Style TargetType="{x:Type ToolTip}"> ) it will be used as the default. 如果MyDefaultStyles包含一个隐式工具提示样式( <Style TargetType="{x:Type ToolTip}"> ),它将用作默认<Style TargetType="{x:Type ToolTip}"> 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 (ie Window, UserControl, single layout Panel): 您还可以通过将样式指定为x:Key,然后在想要应用的任何范围内(例如,Window,UserControl,单一布局面板)创建隐式的ToolTip样式,来更局部地定位它:

<Style TargetType="{x:Type ToolTip}" BasedOn="{StaticResource ExternalLibraryNamedStyle}">...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM