C# 如何将工具提示数据绑定到用户控件的一部分?
我知道这个问题已经被问过很多次了,我在网上找到了一些解决方案,但我无法让它们中的任何一个起作用。我在一个自定义控件上有一些元素,我想在其中一个控件上放一个绑定的工具提示。这就是我目前所拥有的C# 如何将工具提示数据绑定到用户控件的一部分?,c#,.net,wpf,xaml,C#,.net,Wpf,Xaml,我知道这个问题已经被问过很多次了,我在网上找到了一些解决方案,但我无法让它们中的任何一个起作用。我在一个自定义控件上有一些元素,我想在其中一个控件上放一个绑定的工具提示。这就是我目前所拥有的 <UserControl x:Class="DirectGraphicsControl.ColorBarView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="
<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"; }
}
//
///工具提示文本
///
公共静态只读从属属性TooltipProperty=
Register(Reflection.GetPropertyName(m=>m.MyTooltip),
typeof(字符串)、typeof(ColorBarView));
///
///工具提示文本
///
公共字符串MyTooltip
{
获取{return“来自绑定的测试”;}
}
代码隐藏属性现在只返回一个硬编码字符串,直到我能让它真正工作,然后它将返回我想要的计算值。当前,将鼠标悬停在一个空框上。当我将工具提示的文本直接添加到xaml时,它显示的很好。这让我觉得它找不到绑定的位置
我是wpf的新手,所以任何建议都是很好的
谢谢,查看Visual Studio输出选项卡。XAML绑定错误打印在那里,从那里应该很容易发现问题。尝试将dependency属性更改为:
public static readonly DependencyProperty TooltipProperty =
DependencyProperty.Register(Reflection.GetPropertyName<ColorBarView>(m => m.MyTooltip),
typeof(string), typeof(ColorBarView),new PropertyMetadata("Test Value") );
public静态只读从属属性TooltipProperty=
Register(Reflection.GetPropertyName(m=>m.MyTooltip),
typeof(字符串)、typeof(ColorBarView)、新PropertyMetadata(“测试值”);
以及对以下方面的约束:
<Grid DataContext="{Binding ElementName=_myControl, Path=MyTooltip}" >
我现在可以看出,我确实存在找不到绑定的问题。不过,我不知道如何解决这个问题。我原以为找到绑定到属性的代码隐藏文件应该没有问题。以下是“输出”选项卡中的错误:System.Windows.Data错误:40:BindingExpression路径错误:“对象”“DockPanel'(名称=”“U mainDockPanel')上未找到MyTooltip”属性。BindingExpression:Path=MyTooltip;DataItem='DockPanel'(名称=''U mainDockPanel');目标元素为“网格”(名称=“”);目标属性是“DataContext”(类型“Object”),用于删除在输出窗口中发现的绑定错误。虽然我仍然没有在工具提示中获得值,但它只是一个空白框。我感谢您的帮助tillerstarr,谢谢您的回复。。。我刚刚尝试过,现在我又在输出窗口中得到一个错误:System.Windows.Data错误:4:找不到引用'ElementName=\u myControl'绑定的源。BindingExpression:Path=MyTooltip;DataItem=null;目标元素是“工具提示”(名称=“”);目标属性为“Content”(类型为“Object”),这会导致编译时错误。分析标记扩展时遇到类型“System.Windows.Data.RelativeSource”的未知属性“Path”。第18行位置30。“它现在将编译,但在查找绑定源时仍然存在问题:System.Windows.Data错误:4:找不到引用为“RelativeSource FindAncestor”的绑定源,AncestorType='System.Windows.Controls.UserControl',AncestorLevel='1'。BindingExpression:Path=MyTooltip;DataItem=null;目标元素是“工具提示”(名称=“”);目标属性为“Content”(类型为“Object”)仍然是相同的错误:System.Windows.Data错误:4:找不到用于绑定的源,引用为“RelativeSource FindAncestor,AncestorType='System.Windows.Controls.UserControl',AncestorLevel='1'。BindingExpression:Path=MyTooltip;DataItem=null;目标元素是“工具提示”(名称=“”);目标属性为“Content”(类型为“Object”),我想知道控件的设置是否存在根本问题。我从没想到会如此困难。我喜欢xaml,并了解它是如何强大的,但如果在查找绑定源代码时有IDE的帮助,那就太好了。