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,并在实例化后面的代码中设置了 TextBlock 的 DataContext = this
。不过,我想知道如何在 XAML 中解决此问题。
I'm in the process of creating a simple user control; just an ImageButton.
I've already successfully bound the Image to the button and so I've decided to add a tooltip. Now I'm having troubles. It seems that I can hard-code the text for the tooltip in the XAML for the control, but when it's bound it's returning an empty string.
Here's the XAML for my control:
<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>
And here's the dependency property info for the tooltip text:
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);
}
}
And, finally, the declaration of the control in my Window:
<controls:ImageButton x:Name="btnAdd" Source="/DACMS;component/Resources/plus.png" ToolTipText="Add New Item" Click="btnAdd_Click" />
As I mentioned before, the image binds just fine and I've done it in exactly the same manner.
Any ideas?
Thanks,
Sonny
EDIT: I have it working now. I've removed the ElementName from the binding and set the TextBlock's DataContext = this
in the code behind on instanciation. Still, I'd like to know how to fix this in the XAML, instead.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我现在无法对此进行测试,但您可以尝试:
您可能需要对 PlacementTarget 中“Parent”的数量进行一些实验。
希望这有效。我不喜欢给出未经测试的答案,但我这台计算机上没有 VS。 :)
I'm unable to test this right now, but you can try:
You may have to experiment a little with the number of "Parent" in PlacementTarget.
Hopefully this works. I don't like giving answers that I haven't tested, but I don't have VS on this computer. :)
我在绑定到 ContextMenu 时遇到了同样的问题。经过我的研究,我认为这是因为 ToolTip 和 ContextMenu 不存在于页面/窗口/控件的可视树中。因此,DataContext 不是继承的,并且使绑定变得麻烦。
这是我发现对我有用的 Xaml hack。
绑定到菜单项在 WPF 上下文菜单中
I've had this same problem with binding to a ContextMenu. After my research I think that it is because the ToolTip and ContextMenu do not exist within the visual tree of your page/window/control. And therefore the DataContext is not inherited and makes binding troublesome.
Here is a Xaml hack I found that worked for me.
Binding to a MenuItem in a WPF Context Menu
通过 xaml 将数据上下文设置为“this”的方法如下所示:
另一方面,wpf 按钮允许其内容是您想要的任何(单个)内容。如果您想要文本以外的内容(即文本和图像),它看起来像这样:
The way to set the data context to "this" through xaml looks like this:
As another point, wpf buttons allow their content to be just about any (single) thing you want. If you want something other than text (ie, text and an image), it looks like this:
由于除了工具提示 TextBlock 上的文本之外,您没有更改任何内容,因此您只需使用内联声明即可为您生成 TextBlock,并且不需要任何黑客技术来解决您遇到的名称范围问题
:可以交替地在图像上设置工具提示并将控件用作 DataContext,这可以解决名称范围问题。 DataContext 将传递到 ToolTip,允许正常绑定:
这种方式允许在 TextBlock 或更复杂的视觉效果上进行其他设置。
Since you aren't changing anything but the Text on the tooltip TextBlock you can just use an inline declaration which will generate the TextBlock for you and doesn't require any hacking to get around the name scoping issue you're running into otherwise:
You could alternately set the ToolTip on the Image and use the control as the DataContext, which gets around the name scoping problem. The DataContext will be passed to the ToolTip, allowing normal binding:
This way allows additional settings on the TextBlock or more complex visuals.
这修复了工具提示绑定和依赖项属性的问题:
而不是这个(不起作用):
This fixes the Problem with the Tooltip Bindings and Dependencies Properties:
Instead of this ( doesnt Work ):