我正在创建一个简单的用户控件;只是一个ImageButton。WPF - 自定义用户控件上的工具提示文本问题
我已经成功将图像绑定到按钮,所以我决定添加一个工具提示。现在我有麻烦了。看起来我可以在控件的XAML中对工具提示的文本进行硬编码,但是当它被绑定时它会返回一个空字符串。
下面是我的控制XAML:
<Button x:Class="BCOCB.DACMS.Controls.ImageButton"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Name="this"
Style="{StaticResource DisabledButton}">
<Image Source="{Binding ElementName=this, Path=Source}" />
<Button.ToolTip>
<TextBlock Text="{Binding ElementName=this, Path=ToolTipText}" />
</Button.ToolTip>
</Button>
而这里的工具提示文本的依赖项属性信息:
public static readonly DependencyProperty ToolTipTextProperty = DependencyProperty.Register("ToolTipText", typeof(string), typeof(ImageButton));
public string ToolTipText
{
get
{
return this.GetValue(ToolTipTextProperty) as string;
}
set
{
this.SetValue(ToolTipTextProperty, value);
}
}
最后,在我的窗口控制的声明:
<controls:ImageButton x:Name="btnAdd" Source="/DACMS;component/Resources/plus.png" ToolTipText="Add New Item" Click="btnAdd_Click" />
正如我之前提到的,图像绑定很好,我已经完全一样方式。
任何想法?
感谢,
桑尼
编辑:我现在的工作。我从绑定中删除了ElementName,并在Instanciation的后面的代码中设置了TextBlock的DataContext = this
。不过,我想知道如何在XAML中解决这个问题。
非常接近。我最终想到了这一点,但我会因为让我走上正确的道路而给你信心。 我最终将` `改为` ` –
2010-12-22 22:43:15