未显示Adorner上的工具提示 [英] ToolTip on Adorner doesn't show up
问题描述
我的装饰者定义如下:
private class ErrorAdorner : Adorner
{
private readonly Border _errorBorder;
public ErrorAdorner(UIElement adornedElement)
: base(adornedElement)
{
_errorBorder = new Border();
_errorBorder.BorderThickness = new Thickness(2);
_errorBorder.BorderBrush = Brushes.Red;
Image img = new Image();
img.HorizontalAlignment = HorizontalAlignment.Right;
img.VerticalAlignment = VerticalAlignment.Center;
img.Stretch = Stretch.None;
Binding imgBinding = new Binding
{
Source = adornedElement,
Path = new PropertyPath(IconProperty)
};
img.SetBinding(Image.SourceProperty, imgBinding);
Binding ttBinding = new Binding
{
Source = adornedElement,
Path = new PropertyPath(ErrorMessageProperty)
};
img.SetBinding(ToolTipProperty, ttBinding);
_errorBorder.Child = img;
}
protected override Size MeasureOverride(Size constraint)
{
AdornedElement.Measure(constraint);
return AdornedElement.RenderSize;
}
protected override Size ArrangeOverride(Size finalSize)
{
_errorBorder.Arrange(new Rect(finalSize));
return finalSize;
}
protected override Visual GetVisualChild(int index)
{
if (index == 0)
return _errorBorder;
throw new ArgumentOutOfRangeException("index");
}
protected override int VisualChildrenCount
{
get
{
return 1;
}
}
}
ErrorMessage
和 Icon
是在封闭类( ErrorProvider
)中声明的附加属性。当 ErrorMessage
属性设置为非空值时,装饰器将添加到元素。
ErrorMessage
and Icon
are attached properties declared in an enclosing class (ErrorProvider
). The adorner is added to an element when the ErrorMessage
property is set to a non-null value.
我的问题是当装饰器正确渲染时,将鼠标移到图像上时,图像上的 ToolTip
不会显示。我知道这不是一个有约束力的问题:当我使用Snoop检查控件时,可以看到 ToolTip
属性具有预期值。我怀疑问题与点击测试有关,因为我无法在装饰器中收到与鼠标有关的任何事件。 IsHitTestVisible
属性是设置为true,所以我不明白为什么我不接收事件。
My problem is that while the adorner is properly rendered, the ToolTip
on the image doesn't show up when I move the mouse over it. I know it isn't a binding issue: when I examine the controls with Snoop, I can see that the ToolTip
property has the expected value. I suspect the problem is related to hit testing, because I can't receive any mouse related event in the adorner... The IsHitTestVisible
property is set to true, so I don't understand why I don't receive the events.
有什么想法吗?
推荐答案
好吧,这也是以前给我带来痛苦的东西。定义自己的可视化树时,仅返回可视化子级是不够的,还需要告诉WPF您已添加了它们。在构造函数的末尾添加以下内容:
Ok, this is something that has bitten me before also. When you define your own visual tree, it isn't enough to just return the visual children, you also need to tell WPF that you've added them. At the end of your constructor just add this:
this.AddVisualChild(_errorBorder);
this.AddLogicalChild(_errorBorder);
您还应该实现 LogicalChildren
属性:
protected override System.Collections.IEnumerator LogicalChildren
{
get
{
yield return _errorBorder;
}
}
如果您有多个孩子,我会使用 UIElementCollection
。它将它们添加到视觉和逻辑树中,您可以从 LogicalChildren
, VisualChildrenCount
和 GetVisualChild
覆盖。
If you had multiple children, I'd use the UIElementCollection
. It will add them to the visual and logical trees, and you can just use it from the LogicalChildren
, VisualChildrenCount
, and GetVisualChild
overrides.
这篇关于未显示Adorner上的工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!