C# – Wpf: Apply custom style of ToolTip at multiple controls

c++controltemplatestylestooltipwpf

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.

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

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

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>

Any Help plz.

Best Solution

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>

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}">...
Related Question