简体   繁体   English

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

[英]How to databind the tooltip to part of a user control?

I know this question has been asked many times before, I have found a few solutions here on line, but I could not make any of them work. 我知道这个问题之前已被问过很多次了,我在网上找到了一些解决方案,但我无法让它们中的任何一个工作。 I have a few elements on a custom control I have, and I want to put a bound tool tip on one of them. 我在自定义控件上有一些元素,我想在其中一个上添加一个绑定的工具提示。 This is what I have so far. 这就是我到目前为止所拥有的。

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

and this is the code behind. 这就是背后的代码。

/// <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"; }
    }

The code behind property is only returning a hard coded string for now until I can actually get it working and then it will return the calculated value I would like it to. 后面的代码属性只返回一个硬编码的字符串,直到我实际上可以使它工作,然后它将返回我想要的计算值。 Currently on mouse over an empty box appears. 目前鼠标悬停在空框上。 When I add the text for the tooltip straight to the xaml it shows fine. 当我将工具提示的文本直接添加到xaml时,它显示正常。 This makes me think it cannot find the location of the binding. 这让我觉得它无法找到绑定的位置。

I am very new to wpf so any suggestions would be great. 我对wpf很新,所以任何建议都会很棒。

Thank you, 谢谢,

Look in the Visual Studio output tab. 查看Visual Studio输出选项卡。 XAML Binding errors are printed out there, and it should be easy to spot the trouble from there. XAML绑定错误打印在那里,应该很容易从那里发现问题。

Try changing the dependency property to: 尝试将依赖项属性更改为:

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

and the binding to: 并绑定到:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM