UI 元素的扩展工具提示

Extended ToolTip for UI Elements

我对工具提示有疑问,需要一些帮助。 我想为所有工具提示创建自定义设计,添加页眉和页脚。

因为它应该是默认的工具提示 style/template 我创建了附加属性,以便在所有 UI 元素上设置页眉和页脚的值。

public class ToolTipExtensions
{
    public static readonly DependencyProperty HeaderProperty = DependencyProperty.RegisterAttached("Header",
        typeof(string), typeof(ToolTipExtensions), new PropertyMetadata(null));

    public static readonly DependencyProperty FooterProperty = DependencyProperty.RegisterAttached("Footer",
        typeof(string), typeof(ToolTipExtensions), new PropertyMetadata(null));

    public static void SetHeader(UIElement element, string value)
    {
        element.SetValue(HeaderProperty, value);
    }

    public static string GetHeader(UIElement element)
    {
        return (string)element.GetValue(HeaderProperty);
    }

    public static void SetFooter(UIElement element, string value)
    {
        element.SetValue(FooterProperty, value);
    }

    public static string GetFooter(UIElement element)
    {
        return (string)element.GetValue(FooterProperty);
    }
}

我正在使用以下样式。

<Style TargetType="{x:Type ToolTip}">
    <Setter Property="MinHeight" Value="150" />
    <Setter Property="Width" Value="350" />
    <Setter Property="Foreground" Value="Black" />
    <Setter Property="Background" Value="#ECEFF4" />
    <Setter Property="BorderBrush" Value="#394F6D" />

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ToolTip}">
                <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1,1,1,1">
                    <Grid Background="{TemplateBinding Background}">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="40" />
                            <RowDefinition Height="*" />
                            <RowDefinition Height="30" />
                        </Grid.RowDefinitions>

                        <Label
                            Grid.Row="0"
                            MinHeight="40"
                            Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(local:ToolTipExtensions.Header)}"
                            FontFamily="Arial"
                            FontSize="13"
                            FontWeight="Bold"
                            Foreground="{TemplateBinding Foreground}" />

                        <TextBlock
                            Grid.Row="1"
                            MinHeight="40"
                            Foreground="{TemplateBinding Foreground}"
                            Text="{Binding ToolTip, RelativeSource={RelativeSource Mode=TemplatedParent}}" />

                        <Label
                            Grid.Row="2"
                            MinHeight="30"
                            Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(local:ToolTipExtensions.Footer)}"
                            FontFamily="Arial"
                            FontSize="12"
                            FontStyle="Italic"
                            FontWeight="Regular" />
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我做错了什么,因为工具提示没有显示任何内容。没有工具提示,没有页眉,没有页脚。 它有什么问题?工具提示的 DataContext 错误?

用法如下:

<Button
    local:ToolTipExtensions.Header="Button"
    local:ToolTipExtensions.Footer="To show additional Info"
    Content="Test"
    ToolTip="Some fancy Text"
    ToolTipService.InitialShowDelay="500"
    ToolTipService.ShowDuration="999999999" />

此致,

SyLuS

ToolTip for every control will be updated

修改后的样式

<VM:DependecnyObjectTypeConverter x:Key="ConverterPa"/>
    <Style TargetType="{x:Type ToolTip}">
        <Setter Property="MinHeight" Value="150" />
        <Setter Property="Width" Value="350" />
        <Setter Property="Foreground" Value="Black" />
        <Setter Property="Background" Value="#ECEFF4" />
        <Setter Property="BorderBrush" Value="#394F6D" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1,1,1,1">
                        <Grid Background="{TemplateBinding Background}">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="40" />
                                <RowDefinition Height="*" />
                                <RowDefinition Height="30" />
                            </Grid.RowDefinitions>

                            <Label
                        Grid.Row="0"
                        MinHeight="40"
                        Content="{Binding RelativeSource={RelativeSource Mode=TemplatedParent},Converter={StaticResource ConverterPa},ConverterParameter=Header}"
                        FontFamily="Arial"
                        FontSize="13"
                        FontWeight="Bold"
                        Foreground="{TemplateBinding Foreground}" />

                            <TextBlock
                        Grid.Row="1"
                        MinHeight="40"
                        Foreground="{TemplateBinding Foreground}"
                        Text="{Binding RelativeSource={RelativeSource Mode=TemplatedParent},Converter={StaticResource ConverterPa},ConverterParameter=ToolTip}"/>
                            <Label
                        Grid.Row="2"
                        MinHeight="30"
                       Content="{Binding RelativeSource={RelativeSource Mode=TemplatedParent},Converter={StaticResource ConverterPa},ConverterParameter=Footer}"
                        FontFamily="Arial"
                        FontSize="12"
                        FontStyle="Italic"
                        FontWeight="Regular" />
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

IValueConverter:

  public class DependecnyObjectTypeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var plTarget = value as System.Windows.Controls.ToolTip;
            if (parameter.ToString() == "Header")
            {
                return plTarget.PlacementTarget.GetValue(ToolTipExtensions.HeaderProperty);
            }
            if (parameter.ToString() == "ToolTip")
            {
                return plTarget.PlacementTarget.GetValue(Control.ToolTipProperty);
            }
            if (parameter.ToString() == "Footer")
            {
                return plTarget.PlacementTarget.GetValue(ToolTipExtensions.HeaderProperty);
            } 
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }