Adorner 上的工具提示不显示

发布于 2024-11-28 01:21:39 字数 2207 浏览 2 评论 0原文

我有一个装饰器定义如下:

    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;
            }
        }
    }

ErrorMessageIcon 是在封闭类 (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

腻橙味 2024-12-05 01:21:39

好吧,这也是我以前被咬过的东西。当您定义自己的可视化树时,仅返回可视子级是不够的,您还需要告诉 WPF 您已添加它们。在构造函数的末尾添加以下内容:

this.AddVisualChild(_errorBorder);
this.AddLogicalChild(_errorBorder);

您还应该实现 LogicalChildren 属性:

protected override System.Collections.IEnumerator LogicalChildren
{
    get 
    { 
        yield return _errorBorder;
    }
}

如果您有多个子项,我会使用 UIElementCollection。它将它们添加到视觉和逻辑树中,您只需从 LogicalChildrenVisualChildrenCountGetVisualChild 覆盖中使用它即可。

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);

You should also implement the LogicalChildren property:

protected override System.Collections.IEnumerator LogicalChildren
{
    get 
    { 
        yield return _errorBorder;
    }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文