Custom ToolTip template for some elements

为君一笑 提交于 2020-07-22 07:04:08

问题


I want to set custom tooltip style, but ONLY for certains type of controls. I have one custom control that set a tooltip with color depends on what kind of message is binding.

If set style for "Tooltip", ALL tooltips show with no border, but i was looking to set only for my custom control.

<Style x:Key="{x:Type ToolTip}" TargetType="ToolTip">
    <Setter Property="OverridesDefaultStyle" Value="true" />
    <Setter Property="HasDropShadow" Value="True" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ToolTip">
                <ContentPresenter />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Here is my desired look & feel http://i.imgur.com/fG1aCso.png

"Normal tooltip" must show when on mouseover on button, "info tooltip" on over the custom control with blue text, etc

FTI: I try to put the style on the DLL (Where the custom control is defined), but not work. I have the next code on my "assembly.info", so it took the templates from "Themes/Generic.xaml"

[assembly: ThemeInfo(
    ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
    //(used if a resource is not found in the page, 
    // or application resource dictionaries)
    ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
    //(used if a resource is not found in the page, 
    // app, or any theme specific resource dictionaries)
)]

回答1:


Following your current approach, you can try using some DataTrigger listening to the PlacementTarget to check its Type and set the Template accordingly. So we need a Converter here (to convert the PlacementTarget to its Type). Here is the working code:

<Style TargetType="ToolTip">
   <Style.Resources>
      <local:ElementToTypeConverter x:Key="typeConverter"/>
   </Style.Resources>
   <Style.Triggers>
       <DataTrigger Binding="{Binding PlacementTarget, 
                              RelativeSource={RelativeSource Self},
                              Converter={StaticResource typeConverter}}" 
                    Value="{x:Type Button}">
           <Setter Property="Template">
              <Setter.Value>
                 <ControlTemplate TargetType="ToolTip">
                    <Border BorderBrush="Red" BorderThickness="2">
                      <ContentPresenter/>
                    </Border>
                 </ControlTemplate>
              </Setter.Value>
           </Setter>
       </DataTrigger>
       <DataTrigger Binding="{Binding PlacementTarget, 
                              RelativeSource={RelativeSource Self},
                              Converter={StaticResource typeConverter}}" 
                    Value="{x:Type ToggleButton}">
           <Setter Property="Template">
              <Setter.Value>
                 <ControlTemplate TargetType="ToolTip">
                    <Border BorderBrush="Blue" BorderThickness="1">
                       <ContentPresenter/>
                    </Border>
                 </ControlTemplate>
              </Setter.Value>
           </Setter>
       </DataTrigger>
   </Style.Triggers>
</Style>

Codebehind:

public class ElementToTypeConverter : IValueConverter
{

    object IValueConverter.Convert(object value, Type targetType, 
                    object parameter, System.Globalization.CultureInfo culture)
    {
        return value == null ? Type.Missing : value.GetType();
    }

    object IValueConverter.ConvertBack(object value, Type targetType, 
                    object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Above is just an example applied on 2 control types: the Button and the ToggleButton, you can replace them with your own control types and change the ToolTip's Template to whatever you want.



来源:https://stackoverflow.com/questions/26950935/custom-tooltip-template-for-some-elements

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!