2010-11-17 80 views
2

我正在使用儀表板應用程序,我希望讓用戶在畫布上調整窗口小部件的大小。環顧四周,對我來說最好的解決方案似乎是使用微軟的ResizingAdorner類。該示例可以在here找到,代碼可以找到here(大約下一個方向的四分之一)。一切似乎工作,直到我點擊其中一個小部件(來自ComponentOne的圖表控件)。右下角adorner和右上角adorner似乎在移動時出現在畫布邊上的寬度和高度。見下面的例子:C#WPF調整大小問題

alt text alt text

我已經向StackOverflow的問題here有關使用網格分路器,但是這不會爲我工作,因爲控制將是重疊的網格列。

我也到過類似的question,但第一answer沒有在所有的工作,而第二answer只是僅僅指向blog其中紳士無論是適用於微軟,並建立了ResizingAdorner類或剛剛複製的代碼來自wpf樣本網站。我也嘗試過修改代碼here,但沒有運氣。 有速戰速決,我沒有看到

回答

1

當尋找到代碼深入一點,我發現這是減去所需的寬度和高度x和y的部分,甚至認爲我是不是拖動裝飾者。所以我在他們爲榜樣改變了下面的代碼:

protected override Size ArrangeOverride(Size finalSize) 
     { 
      // desiredWidth and desiredHeight are the width and height of the element that's being adorned. 
      // These will be used to place the ResizingAdorner at the corners of the adorned element. 
      double desiredWidth = AdornedElement.DesiredSize.Width; 
      double desiredHeight = AdornedElement.DesiredSize.Height; 
      // adornerWidth & adornerHeight are used for placement as well. 
      double adornerWidth = this.DesiredSize.Width; 
      double adornerHeight = this.DesiredSize.Height; 

      topLeft.Arrange(new Rect(-adornerWidth/2, -adornerHeight/2, adornerWidth, adornerHeight)); 
      topRight.Arrange(new Rect(desiredWidth - adornerWidth/2, -adornerHeight/2, adornerWidth, adornerHeight)); 
      bottomLeft.Arrange(new Rect(-adornerWidth/2, desiredHeight - adornerHeight/2, adornerWidth, adornerHeight)); 
      bottomRight.Arrange(new Rect(desiredWidth - adornerWidth/2, desiredHeight - adornerHeight/2, adornerWidth, adornerHeight)); 

      // Return the final size. 
      return finalSize; 
     } 

下面的代碼:

protected override Size ArrangeOverride(Size finalSize) 
     { 
      // desiredWidth and desiredHeight are the width and height of the element that's being adorned. 
      // These will be used to place the ResizingAdorner at the corners of the adorned element. 
      double desiredWidth = AdornedElement.DesiredSize.Width; 
      double desiredHeight = AdornedElement.DesiredSize.Height; 
      // adornerWidth & adornerHeight are used for placement as well. 
      double adornerWidth = this.DesiredSize.Width; 
      double adornerHeight = this.DesiredSize.Height; 

      //Orginal Microsoft code 
      //topLeft.Arrange(new Rect(-adornerWidth/2, -adornerHeight/2, adornerWidth, adornerHeight)); 
      //topRight.Arrange(new Rect(desiredWidth - (adornerWidth/2), - adornerHeight/2, adornerWidth, adornerHeight)); 
      //bottomLeft.Arrange(new Rect(-adornerWidth/2, desiredHeight - adornerHeight/2, adornerWidth, adornerHeight)); 
      //bottomRight.Arrange(new Rect(desiredWidth - (adornerWidth/2), desiredHeight - adornerHeight/2, adornerWidth, adornerHeight)); 


      topLeft.Arrange(new Rect(-adornerWidth/2, -adornerHeight/2, adornerWidth, adornerHeight)); 
      topRight.Arrange(new Rect(adornerWidth/2, -adornerHeight/2, adornerWidth, adornerHeight)); 
      bottomLeft.Arrange(new Rect(-adornerWidth/2, adornerHeight/2, adornerWidth, adornerHeight)); 
      bottomRight.Arrange(new Rect(adornerWidth/2, adornerHeight/2, adornerWidth, adornerHeight)); 

      // Return the final size. 
      return finalSize; 
     } 

我沒有經歷過任何怪癖還,但它似乎是正確的。