簡體   English   中英

WPF:在多個控件中應用自定義樣式的工具提示

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

我正在使用WPF應用程序。 我創建了一個自定義控件庫,在其中自定義了所有控件,這意味着添加了一些功能並重新設置了它們的樣式。 同樣,我也重新設置了工具提示的樣式。 我正在其他項目中使用此自定義庫。 除ToolTip之外,其他一切都正常。 工具提示樣式未得到應用。 任何幫助。

編輯:

我創建了一個名為ToolTip的自定義類,該類從System.Windows.Controls.ToolTip派生,我也為自定義類聲明了樣式。 現在,我想知道如何將這種樣式應用於每個控件的工具提示,這意味着我想在用戶在控件上設置工具提示時創建我的工具提示的對象。

在.cs中:

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

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

任何幫助。

如果您的自定義ToolTip類僅是為了應用模板,則對工具提示使用隱式樣式會容易得多:

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

將默認的工具提示樣式放在單獨的程序集中的共享ResourceDictionary中,將允許多個項目使用它。 您可以使用以下命令將SharedStyleLibrary.dll的Resources文件夾中的MyDefaultStyles ResourceDictionary合並到App.xaml中:

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

如果MyDefaultStyles包含一個隱式工具提示樣式( <Style TargetType="{x:Type ToolTip}"> ),它將用作默認<Style TargetType="{x:Type ToolTip}"> 您還可以通過將樣式指定為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