在工具提示中绑定到文本块的wpf自定义控件依赖项属性?

时间:2018-06-19 16:35:05

标签: c# wpf user-controls tooltip

因此,我正在创建一个自定义控件,以允许我自己重复使用带有工具提示并带有自定义消息的fontawesome图标。我有一个称为Message的依赖项属性,另一个名为ToolTipType的依赖项属性(这使控件成为信息或警告工具提示,并且样式会根据选择内容而略有变化)。因为ToolTipType绑定到fontawesome控件上的样式触发器,所以它工作得很好。但是Message属性绑定到<“ fontawesome.tooltip”>内部的textblock文本属性。在我的研究中,我知道工具提示不在可视化树中,这就是为什么FindAncestor无法正常工作。我在这篇here帖子中找到了解决方案。尽管这对我的情况没有帮助,因为绑定不是直接在工具提示上,而是在工具提示内的文本块控件上。

这是我的代码:

<UserControl x:Class="WardKraft.DesktopUI.Controls.UnityToolTip"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:fa="http://schemas.fontawesome.io/icons/"
         xmlns:local="clr-namespace:WardKraft.DesktopUI.Controls"
         mc:Ignorable="d" 
         d:DesignHeight="450" d:DesignWidth="800">
<Grid>
    <fa:FontAwesome 
        Grid.Column="0" 
        Icon="InfoCircle" 
        Foreground="#FF24456a"
        VerticalAlignment="Center"
        Margin="0 0 5 0">
        <fa:FontAwesome.Style>
            <Style TargetType="fa:FontAwesome">
                <Setter Property="Visibility" Value="Collapsed"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, 
                    Path=ToolTipType}" Value="Info">
                        <Setter Property="Visibility" Value="Visible"></Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </fa:FontAwesome.Style>
        <fa:FontAwesome.ToolTip>
            <StackPanel> 
                <Border 
                    CornerRadius="15"
                    BorderThickness="1"
                    BorderBrush="#FFF"
                    Background="#FF24456a"
                    Width="400">
                    <TextBlock 
                        Background="#FF24456a"
                        Padding="0 3"
                        FontWeight="Bold"
                        FontSize="16" 
                        Foreground="White" 
                        HorizontalAlignment="Center"
                        Text="Info"/>
                </Border>
                <TextBlock 
                    Name="TxtInfoBody"
                    Foreground="#FF24456a"
                    TextWrapping="Wrap"
                    Margin="8 10"
                    Padding="5"
                    FontSize="14"
                    Text="{Binding Path=Message, 
                                   Mode=TwoWay, 
                                   RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:UnityToolTip}}}"/>
            </StackPanel>
        </fa:FontAwesome.ToolTip>
    </fa:FontAwesome>
    <fa:FontAwesome 
        Grid.Column="0" 
        Icon="ExclamationTriangle" 
        Foreground="#DF6E1E"
        VerticalAlignment="Center"
        Margin="0 0 5 0">
        <fa:FontAwesome.Style>
            <Style TargetType="fa:FontAwesome">
                <Setter Property="Visibility" Value="Collapsed"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, 
                    Path=ToolTipType}" Value="Warning">
                        <Setter Property="Visibility" Value="Visible"></Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </fa:FontAwesome.Style>
        <fa:FontAwesome.ToolTip>
            <StackPanel>
                <Border 
                    CornerRadius="15"
                    BorderThickness="1"
                    BorderBrush="#DF6E1E"
                    Background="#DF6E1E">
                    <TextBlock 
                        Background="#DF6E1E"
                        Padding="0 3"
                        Width="400"
                        FontWeight="Bold"
                        FontSize="16" 
                        Foreground="White" 
                        HorizontalAlignment="Center"
                        Text="Warning"/>
                </Border>
                <TextBlock 
                    Foreground="#FF24456a"
                    Margin="8 10"
                    Padding="5"
                    FontSize="14"
                    Text="{Binding Path=Message, 
                                   RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"/>
            </StackPanel>
        </fa:FontAwesome.ToolTip>
    </fa:FontAwesome>
</Grid>

这是背后的代码:

命名空间WardKraft.DesktopUI.Controls {     公共局部类UnityToolTip:UserControl     {         公共UnityToolTip()         {             InitializeComponent();         }

    public static readonly DependencyProperty MessageProperty =
        DependencyProperty.Register(nameof(Message), typeof(string), typeof(UnityToolTip), new PropertyMetadata("Message goes here."));

    public static readonly DependencyProperty ToolTipTypeProperty =
        DependencyProperty.Register(nameof(ToolTipType), typeof(ToolTipTypes), typeof(UnityToolTip), new PropertyMetadata(ToolTipTypes.Info));


    public string Message
    {
        get => (string) GetValue(MessageProperty);
        set => SetValue(MessageProperty, value);
    }

    public ToolTipTypes ToolTipType
    {
        get => (ToolTipTypes)GetValue(ToolTipTypeProperty);
        set => SetValue(ToolTipTypeProperty, value);
    }

    public enum ToolTipTypes
    {
        [Description("Info")]
        Info = 1,
        [Description("Warning")]
        Warning = 2
    }
}

}

0 个答案:

没有答案