UI 元素的扩展工具提示



我在使用工具提示时遇到问题,需要一些帮助。我想为所有工具提示创建自定义设计,添加页眉和页脚。

因为它应该是我创建附加属性的默认工具提示样式/模板,所以在所有 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>

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

这是用法:

<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

将更新每个控件的工具提示

修改后的样式

<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>

IValue转换器:

  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();
        }
    }

相关内容

  • 没有找到相关文章

最新更新