c# - Wpf:在多个控件上应用自定义样式的工具提示

标签 c# wpf tooltip styles controltemplate

我正在使用 WPF 应用程序。我创建了一个自定义控件库,在其中自定义了所有控件,这意味着添加了一些功能并重新设计了它们的样式。我也以同样的方式重新设计了工具提示。我在其他项目中使用这个自定义库。除了工具提示之外,一切都工作正常。工具提示样式未得到应用。请提供任何帮助。

编辑:

我创建了一个名为 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>

请提供任何帮助。

最佳答案

将默认的工具提示样式放置在单独程序集中的共享 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}"> ) 它将被用作默认值。您还可以通过为样式指定 x:Key,然后在您希望其应用的任何范围(即窗口、用户控件、单一布局面板)中创建隐式工具提示样式来更本地化地定位它:

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

关于c# - Wpf:在多个控件上应用自定义样式的工具提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4385068/

相关文章:

C# 能力?

c# - 如何使用 string.contains 查找分隔/隔离的子字符串?

wpf - 无法序列化泛型类型 'System.Windows.FreezableCollection`

java - 将鼠标悬停在 JButton 上并显示一条消息

html - 悬停时弹出窗口不显示(工具提示)

javascript - 在 chartjs-2 中更改工具提示的 z 索引

c# - 如何用 func 委托(delegate)替换 Action 委托(delegate)将值返回给函数

c# - 无法使用 smo 从 C# 执行脚本

c# - 删除资源字典中未使用的样式

c# - 带有资源文件的居中 DataGridTextColumn 标题中的换行符