开发者

Wpf: Apply custom style of ToolTip at multiple controls

开发者 https://www.devze.com 2023-01-29 15:51 出处:网络
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 ToolTi

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 Radiu开发者_Python百科sX="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.


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


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:

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

精彩评论

暂无评论...
验证码 换一张
取 消