我正在尝试根据装饰元素的父级尺寸来定位装配工。例如,我有一个文本框。我想装饰这个文本框,所以它看起来像这样:
how the adorner needs to be placed http://img707.imageshack.us/img707/9840/fig1.png
文本框放置在画布对象中,如果有足够的可用空间,则将装饰器(半透明的圆角正方形)与文本框的下边缘对齐。当用户点击文本框时,会启动装饰器。
目前,画布及其内容(文本框)以WinForms格式托管 - 因此WPF由ElementHost控件处理。
但是当我运行我的代码时,第一次单击文本框时,它会显示与文本框顶边对齐的装饰器(参见下图)。之后它正确定位(如上图所示)有谁知道为什么会这样?
how adorner is positions 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;
}
}
}
答案 0 :(得分:1)
装饰者的位置与装饰元素有关。如果您希望它位于对象的顶部,则yloc
的值应为负数。但是,您所使用的代码也会考虑Canvas的边界。如果上面的矩形没有足够的位置,它会把它放在下面。尝试将TextBox放置在Canvas中稍低的位置。