WPF 中的常用工具提示样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17228954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:03:11  来源:igfitidea点击:

Common Tooltip style in WPF

wpfwpf-controlstooltip

提问by Hardik

Can I make a tooltip style which can be applied to all tooltips for every control.

我可以制作一个可以应用于每个控件的所有工具提示的工具提示样式吗?

I tried this.

我试过这个。

<Style TargetType="{x:Type ToolTip}" >
        <Setter Property="OverridesDefaultStyle" Value="true" />
        <Setter Property="HasDropShadow" Value="True" />
        <Setter Property="Foreground" Value="White" />
        <Setter Property="FontSize" Value="12" />
        <Setter Property="Placement" Value="Bottom" />
        <Setter Property="VerticalOffset" Value="0" />
        <Setter Property="Padding" Value="8" />

        <Setter Property="HorizontalContentAlignment" Value="Center" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ToolTip}" >
                    <StackPanel Margin="7,1" >
                      <Border Background="#FFF7F7CC" CornerRadius="1" >
                            <TextBlock Margin="1" Foreground="Black" HorizontalAlignment="Center" VerticalAlignment="Top" Text="{TemplateBinding ToolTip}"/>
                        </Border>
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

For Using this style I have to put a seperate Tooltip tag in control, eg to apply tooltip to border,

对于使用这种样式,我必须控制一个单独的 Tooltip 标签,例如将工具提示应用于边框,

<Border>
    <Border.ToolTip>
          <ToolTip ToolTip="This is tooltip text"  />
    </Border.ToolTip>
........
.........
</Border>

but is there any way where tooltipstyle applies to all control with tooltip mentioned in same tag. eg.

但是有什么方法可以将 tooltipstyle 应用到所有在同一标签中提到的带有工具提示的控件。例如。

<Border BorderBrush="Transparent" Background="Transparent" Cursor="Help" ToolTip="This is Tooltip" >
.....
.....
</Border>

let me know if any further details are required. Thanks in Anticipation.

如果需要任何进一步的细节,请告诉我。感谢期待。

采纳答案by Jawahar

Yes Your approach will work. But a small change is needed in the Control Template. Replace the TextBlock with ContentPresenter.

是的,您的方法会奏效。但是需要在控制模板中进行一些小的更改。用 ContentPresenter 替换 TextBlock。

                <ControlTemplate TargetType="{x:Type ToolTip}" >
                    <StackPanel Margin="7,1" >
                        <Border Background="#FFF7F7CC" CornerRadius="1" >
                        <ContentPresenter Margin="1" HorizontalAlignment="Center" VerticalAlignment="Top" />
                        </Border>
                    </StackPanel>
                </ControlTemplate>