C#WPF调整大小问题

时间:2010-11-17 18:06:02

标签: c# wpf resize

我正在开发一个仪表板应用程序,我想让用户在画布上调整小部件的大小。环顾四周,我的最佳解决方案似乎是使用Microsoft的ResizingAdorner类。可以找到示例here,可以找到代码here(大约四分之一的页面)。在我点击其中一个小部件(来自ComponentOne的图表控件)之前,一切似乎都有效。右下角adorner和右上角adorner似乎在移动时出现在画布一侧的宽度和高度上。见下面的例子:

alt text alt text

关于使用网格分割器,我一直在使用StackOverflow问题here,但这对我不起作用,因为控件将重叠网格列。

我也去过类似的question,但第一个answer根本不起作用,而第二个answer只是指向一个blog绅士要么为微软工作,要么创建ResizingAdorner类,要么只是从wpf示例站点复制代码。我也试过修改代码here,但没有运气。 有没有快速修复我没有看到

1 个答案:

答案 0 :(得分: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;
        }

我还没有遇到任何怪癖,但似乎没错。