Son*_*Boy 5 c# wpf xaml binding user-controls
我正在创建一个简单的用户控件; 只是一个ImageButton.
我已经成功将图像绑定到按钮,所以我决定添加一个工具提示.现在我遇到了麻烦.似乎我可以在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>
Run Code Online (Sandbox Code Playgroud)
这是工具提示文本的依赖属性信息:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
最后,在我的窗口中声明控件:
<controls:ImageButton x:Name="btnAdd" Source="/DACMS;component/Resources/plus.png" ToolTipText="Add New Item" Click="btnAdd_Click" />
Run Code Online (Sandbox Code Playgroud)
正如我之前提到的,图像绑定得很好,我以完全相同的方式完成了它.
有任何想法吗?
谢谢,
桑尼
编辑:我现在有工作.我已经从绑定中删除了ElementName,并DataContext = this在instanciation后面的代码中设置了TextBlock .不过,我想知道如何在XAML中解决这个问题.
我现在无法测试,但您可以尝试:
<Button.ToolTip
DataContext=”{Binding Path=PlacementTarget.Parent.Parent,
RelativeSource={x:Static RelativeSource.Self}}"
>
<TextBlock Text="{Binding Path=ToolTipText}" />
</Button.ToolTip>
Run Code Online (Sandbox Code Playgroud)
您可能需要在PlacementTarget中尝试使用"Parent"的数量.
希望这有效.我不喜欢给出我没有测试过的答案,但我没有在这台电脑上安装VS. :)
| 归档时间: |
|
| 查看次数: |
8786 次 |
| 最近记录: |