在WPF中相对于父元素的尺寸定位装饰器

6
我试图根据装饰元素的父级尺寸来定位 Adorner 位置。例如,我有一个文本框,我想要装饰这个文本框,使其看起来像这样: 如何放置装饰器 http://img707.imageshack.us/img707/9840/fig1.png 将文本框放在画布对象中,如果有足够的空间,则将装饰器(半透明圆角矩形)与文本框底部对齐。用户单击文本框时启动装饰器。
当前,画布及其内容(文本框)托管在 WinForms 表单中,因此 WPF 由 ElementHost 控件处理。
但是当我运行我的代码时,第一次单击文本框时,它会将装饰器对齐到文本框的顶部边缘(见下图)。之后,它会正确地定位自己(如上图)。是否有人知道这可能是为什么?

如何定位修饰器 http://img14.imageshack.us/img14/4766/fig2v.png

我已经将代码粘贴在下面:

TextBoxAdorner.cs - 这是修饰器的逻辑

public class TextBoxAdorner : Adorner
{
    private TextBox _adornedElement;
    private VisualCollection _visualChildren;
    private Rectangle _shape;
    private Canvas _container;
    private Canvas _parentCanvas;

    public TextBoxAdorner(UIElement adornedElement, Canvas parentCanvas)
        : base(adornedElement)
    {
        _adornedElement = (TextBox)adornedElement;
        _parentCanvas = parentCanvas;
        _visualChildren = new VisualCollection(this);

        _container = new Canvas();

        _shape = new Rectangle();
        _shape.Width = 100;
        _shape.Height = 80;
        _shape.Fill = Brushes.Blue;
        _shape.Opacity = 0.5;

        _container.Children.Add(_shape);

        _visualChildren.Add(_container);
    }

    protected override Size ArrangeOverride(Size finalSize)
    {
        Point location = GetLocation();
        _container.Arrange(new Rect(location, finalSize));

        return finalSize;
    }

    private Point GetLocation()
    {
        if (_parentCanvas == null)
            return new Point(0, 0);

        Point translate;
        double xloc = 0, yloc = _shape.Height - _adornedElement.ActualHeight;

        if (yloc < 0) // textbox is bigger than the shape
            yloc = 0;
        else
        {
            translate = this.TranslatePoint(new Point(0, -yloc), _parentCanvas);

            // coordinate is beyond the position of the parent canvas
            if (translate.Y < 0)  // this is true the first time it's run
                yloc = 0;
            else
                yloc = -yloc;
        }

        translate = this.TranslatePoint(new Point(_shape.Width, 0), _parentCanvas);

        // textbox is in right edge of the canvas
        if (translate.X > _parentCanvas.ActualWidth) 
        {
            double pos = translate.X - _parentCanvas.ActualWidth;

            translate = this.TranslatePoint(new Point(-pos,0), _parentCanvas);

            if (translate.X < 0)
                xloc = 0;
            else
                xloc = translate.X;
        }

        return new Point(xloc, yloc);
    }

    protected override Size MeasureOverride(Size constraint)
    {
        Size myConstraint = new Size(_shape.Width, _shape.Height);
        _container.Measure(myConstraint);

        return _container.DesiredSize;
    }

    protected override Visual GetVisualChild(int index)
    {
        return _visualChildren[index];
    }

    protected override int VisualChildrenCount
    {
        get
        {
            return _visualChildren.Count;
        }
    }
}
1个回答

1

装饰器的位置是相对于被装饰元素的。如果您希望它位于对象的顶部,则yloc的值应为负数。但是,您所拥有的代码还考虑了画布的边界。如果上方没有足够的空间放置矩形,则会将其放置在下方。尝试将文本框放置在画布中稍低的位置。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接