更改工具提示样式模板

本文关键字:样式 工具提示 | 更新日期: 2023-09-27 18:13:22

我希望能够为其他控件的样式分配不同的工具提示样式

例如,我有我的工具提示样式:

 <Style x:Key="BorderedInfoTooltip" TargetType="{x:Type ToolTip}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ToolTip}">
                    <Border Background="DarkSlateGray" BorderBrush="DarkGoldenrod" BorderThickness="1" CornerRadius="2">
                        <StackPanel Orientation="Horizontal">
                            <Image Width="32" Height="32" Source="Resources/info.png" />
                            <ContentPresenter Content="{TemplateBinding Content}"/>
                        </StackPanel>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

图标样式:

    <Style x:Key="IconStyle" TargetType="Button">
        <Setter Property="VerticalAlignment" Value="Center"></Setter>
        <Setter Property="HorizontalAlignment" Value="Center"></Setter>
        <Setter Property="Width" Value="20"></Setter>
        <Setter Property="Height" Value="20"></Setter>
    </Style>

如何使这个按钮(与style == IconStyle)有我的工具提示风格?

更改工具提示样式模板

不要将工具提示放在iconStyle之外,而是将其放在图像样式资源中,并像这样跳过键:

<Style x:Key="IconStyle" TargetType="Image">
  <Style.Resources>
    <Style TargetType="{x:Type ToolTip}">
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="{x:Type ToolTip}">
            <Border Background="DarkSlateGray" BorderBrush="DarkGoldenrod" BorderThickness="1" CornerRadius="2">
              <StackPanel Orientation="Horizontal">
                <Image Width="32" Height="32" Source="Resources/info.png" />
                <ContentPresenter Content="{TemplateBinding Content}"/>
              </StackPanel>
            </Border>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>
  </Style.Resources>
  <Setter Property="VerticalAlignment" Value="Center"></Setter>
  <Setter Property="HorizontalAlignment" Value="Center"></Setter>
  <Setter Property="Width" Value="20"></Setter>
  <Setter Property="Height" Value="20"></Setter>
</Style>
编辑:

或者你也可以将其保留为全局或Windows资源,并在图标样式资源中引用它,如下所示:

<Style x:Key="IconStyle" TargetType="Image">
  <Style.Resources>
    <Style TargetType="ToolTip" BasedOn="{StaticResource BorderedInfoTooltip}" />
  </Style.Resources>
...

为什么你不能像这样设置你的图像样式:

<Image Width="32" Height="32" Source="Resources/info.png" Style="{StaticResource IconStyle}"/>