我知道这个问题之前已经被问过很多次,我在网上找到了一些解决方案,但是我无法让它们中的任何一个工作。我有一个自定义控件上的几个元素,我想在其中一个元素上放置一个绑定的工具提示。目前为止,我已经做到了这一点。
<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非常陌生,因此任何建议都将是很好的。
谢谢您!