C# WPF 调整大小问题

标签 c# wpf resize

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

alt text alt text

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

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

最佳答案

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

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

关于C# WPF 调整大小问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4207580/

相关文章:

c# - Asp.Net 中的自定义消息框

c# - 处理从主窗口中的模态对话框引发的冒泡事件

android - 上传前调整图片大小(我上传的 take/pick 没问题)

Java - 如何在不裁剪的情况下填充图像并调整图像大小?

android - 裁剪和调整图像大小以在列表中使用

c# - 选择数组 C# 的替代项

javascript - 我的数据库数据未显示在 jQuery 数据表中

c# - 在 System.Windows.Controls.Image 控件中显示 System.Drawing.Bitmap 对象

wpf - MVVM 消息传递或事件或其他选项?

c# - 使用代码删除 WPF 中的绑定(bind)