如何将工具提示数据绑定到用户控件的一部分?

3

我知道这个问题之前已经被问过很多次,我在网上找到了一些解决方案,但是我无法让它们中的任何一个工作。我有一个自定义控件上的几个元素,我想在其中一个元素上放置一个绑定的工具提示。目前为止,我已经做到了这一点。

<UserControl x:Class="DirectGraphicsControl.ColorBarView"
         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:DirectGraphicsControl="clr-namespace:Windows.DirectGraphicsControl" mc:Ignorable="d" 
         SizeChanged="UserControlSizeChanged" MinWidth="95">
<DockPanel LastChildFill="True" Width="80" x:Name="_mainDockPanel">
    <TextBlock DockPanel.Dock="Top" x:Name="_title" x:FieldModifier="public" HorizontalAlignment="Center" Margin="0,2,0,2"/>
    <StackPanel Orientation="Vertical" DockPanel.Dock="Bottom" Margin="0,2,0,2"
                    x:Name="_bottomLabelsRegion" x:FieldModifier="public">
        <TextBlock x:Name="_unitName" x:FieldModifier="public" HorizontalAlignment="Center" VerticalAlignment="Bottom"/>
    </StackPanel>
    <Grid DataContext="{Binding ElementName=_mainDockPanel.Parent, Path=MyTooltip}" >
        <DirectGraphicsControl:DirectGraphicsControl x:Name="_directGraphicsControl"
                                                     MouseDoubleClick="HandleColorBarMouseDoubleClick" x:FieldModifier="public">
            <DirectGraphicsControl:DirectGraphicsControl.ToolTip>
                <ToolTip Content="{Binding Path=PlacementTarget.DataContext, 
                                   RelativeSource={RelativeSource Self}}"/>
            </DirectGraphicsControl:DirectGraphicsControl.ToolTip>
        </DirectGraphicsControl:DirectGraphicsControl>
    </Grid>
</DockPanel>

这是代码的后台。

/// <summary>
    /// The tool tip text
    /// </summary>
    public static readonly DependencyProperty TooltipProperty =
        DependencyProperty.Register(Reflection.GetPropertyName<ColorBarView>(m => m.MyTooltip),
                                    typeof(string), typeof(ColorBarView));

    /// <summary>
    /// The tool tip text
    /// </summary>
    public string MyTooltip
    {
        get { return "Test from binding"; }
    }

目前属性的代码仅返回硬编码字符串,直到我能够实际使其工作,然后它将返回我所需要的计算值。当前在鼠标悬停时会出现一个空框。当我将工具提示的文本直接添加到xaml中时,它就正常显示了。这让我认为它无法找到绑定的位置。

我对wpf非常陌生,因此任何建议都将是很好的。

谢谢您!

2个回答

1

尝试将依赖属性更改为:

 public static readonly DependencyProperty TooltipProperty =
    DependencyProperty.Register(Reflection.GetPropertyName<ColorBarView>(m => m.MyTooltip),
                                typeof(string), typeof(ColorBarView),new PropertyMetadata("Test Value") );

以及绑定到:

<Grid DataContext="{Binding ElementName=_myControl, Path=MyTooltip}" >

那个方法成功地解决了输出窗口中发现的绑定错误。虽然我仍然无法在工具提示中获得值,但它只是一个空白框。 - scott lafoy
感谢您的帮助tillerstarr,非常感谢您的回复...我刚刚尝试了一下,现在又回到了输出窗口出现错误的状态:System.Windows.Data Error: 4 : 找不到绑定的源,引用为'ElementName=_myControl'。BindingExpression:Path=MyTooltip; DataItem=null; target element is 'ToolTip' (Name=''); target property is 'Content' (type 'Object')。 - scott lafoy
这会导致编译时出错。在解析标记扩展时遇到了类型为“System.Windows.Data.RelativeSource”的未知属性“Path”。第18行第30个位置。 - scott lafoy
现在它可以编译了,但仍然存在一个找不到绑定源的问题:System.Windows.Data Error: 4 : 找不到引用'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.UserControl', AncestorLevel='1''的绑定源。BindingExpression:Path=MyTooltip; DataItem=null; 目标元素是'ToolTip' (Name=''); 目标属性是'Content' (类型为'Object')。 - scott lafoy
让我们在聊天中继续这个讨论:http://chat.stackoverflow.com/rooms/19116/discussion-between-tillerstarr-and-scott-lafoy - tillerstarr
显示剩余3条评论

1

在Visual Studio的输出选项卡中查看。XAML绑定错误会在那里打印出来,从那里很容易发现问题所在。


我现在看到了一个问题,它找不到绑定。但我不确定如何解决这个问题。我本以为将代码后台文件绑定到属性上不应该有任何问题。以下是输出选项卡中的错误信息:System.Windows.Data Error: 40 : BindingExpression path error: 'MyTooltip' property not found on 'object' ''DockPanel' (Name='_mainDockPanel')'. BindingExpression:Path=MyTooltip; DataItem='DockPanel' (Name='_mainDockPanel'); target element is 'Grid' (Name=''); target property is 'DataContext' (type 'Object')。 - scott lafoy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接