2010-11-17 84 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; 
     } 

我没有经历过任何怪癖还,但它似乎是正确的。