2010-01-05 58 views
5

我想根據裝飾元素的父級維度來定位裝飾器。例如,我有一個文本框。我想,所以它看起來像這樣來裝飾這個文本框:在WPF中相對於父級維度定位裝飾器

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

裝飾器的位置是相對於所述佐餐元件。如果您希望它位於對象頂部,則yloc的值應爲負值。但是,您也有關於Canvas邊界的代碼。如果上面的矩形沒有足夠的位置,它會放在下面。嘗試將TextBox稍微放置在Canvas中。

相關問題