C# WPF 调整大小问题

发布于 2024-10-03 17:07:30 字数 1672 浏览 4 评论 0 原文

我正在开发一个仪表板应用程序,我想让用户在画布上调整小部件的大小。环顾四周,对我来说最好的解决方案似乎是使用 Microsoft 的 ResizingAdorner 类。该示例可以在此处找到,代码可以在此处(大约页面下方四分之一处)。一切似乎都正常,直到我单击其中一个小部件(来自 ComponentOne 的图表控件)。右下角装饰器和右上角装饰器似乎出现在大约的宽度和从侧面的高度移动时的画布。请参阅下面的示例:

alt text alt text

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

我也遇到过类似的问题,但第一个答案根本不起作用,第二个答案仅仅指向博客,这位先生要么为微软工作并创建了 ResizingAdorner 类,要么只是复制了来自 wpf 示例站点的代码。我也尝试过修改代码这里< /a> 但没有运气。 有没有我没有看到的快速修复

I am working on a dashboard application where I would like to let the user resize the widgets on a canvas. Looking around, the best solution to me seemed to use Microsoft's ResizingAdorner class. The example can be found here and the code can be found here (About a quarter way down the page). Everything seemed to work until I clicked one of the widgets (chart controls from ComponentOne). The bottom right adorner and the top right adorner seemed to appear about the width of the and height from the side of the canvas when ever moved. See example below :

alt text
alt text

I have been to the StackOverflow question here about using a grid splitter, but this will not work for me since controls will be overlapping grid columns.

I also have been to a similar question, but the first answer does not work at all, and the second answer just merely points to a blog where the gentleman either works for microsoft and created the ResizingAdorner class or just copied the code from the wpf samples site. I have also tried is revised code here but with no luck.
Is there a quick fix i'm not seeing

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风吹过旳痕迹 2024-10-10 17:07:30

当更深入地研究代码时,我发现有一部分从所需的宽度和高度中减去了 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;
        }

我还没有遇到任何怪癖,但这似乎是正确的。

When looking into the code a little deeper, I found a portion that was subtracting the x and y from the desired width and height, even thought I wasn't dragging the adorner yet. so I changed the following code in their example :

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;
        }

to the following code:

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;
        }

I haven't experienced any quirks yet, but it seems right.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文