Adorner 上的工具提示不显示
我有一个装饰器定义如下:
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
属性设置为非空值时,装饰器会添加到元素中。
我的问题是,虽然装饰器已正确渲染,但当我将鼠标移到图像上时,图像上的 ToolTip
不会显示。我知道这不是一个绑定问题:当我使用 Snoop 检查控件时,我可以看到 ToolTip
属性具有预期值。我怀疑问题与命中测试有关,因为我无法在装饰器中接收任何与鼠标相关的事件...IsHitTestVisible
属性设置为 true,所以我不明白为什么我没有收到事件。
有什么想法吗?
I have an adorner defined as follows:
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
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.
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.
Any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,这也是我以前被咬过的东西。当您定义自己的可视化树时,仅返回可视子级是不够的,您还需要告诉 WPF 您已添加它们。在构造函数的末尾添加以下内容:
您还应该实现
LogicalChildren
属性:如果您有多个子项,我会使用
UIElementCollection
。它将它们添加到视觉和逻辑树中,您只需从LogicalChildren
、VisualChildrenCount
和GetVisualChild
覆盖中使用它即可。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:
You should also implement the
LogicalChildren
property: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 theLogicalChildren
,VisualChildrenCount
, andGetVisualChild
overrides.