WPF 可拖动面板类

发布于 2024-08-16 09:52:38 字数 3724 浏览 2 评论 0原文

我正在尝试从这两个资源编写一个面板类:

面板类将有两个附加属性“X”和“Y”,如果任何元素给出 x 和 y为零,则它将被放置在面板的中心。该面板还允许用户拖动东西。请帮我写这门课。我对 WPF 很陌生。


这就是我已经走了多远。现在我尝试实现这个,但它不起作用,如果你能帮我实现 GetTop、GetLeft、GetBottom、GetRight 函数,这些函数默认情况下不在面板类中定义,但这是必需的。如果有这4个方法,那么这里就可以实现拖拽功能了。

using System;
using System.Linq;
using System.Windows;
using System.ComponentModel;
using System.Windows.Controls;
using System.Windows.Media;

namespace SmartERP.Elements
{
    public class SmartCanvas : Panel
    {
        public static readonly DependencyProperty TopProperty;
        public static readonly DependencyProperty LeftProperty;
        public static readonly DependencyProperty BottomProperty;
        public static readonly DependencyProperty RightProperty;

        static SmartCanvas()
        {
            TopProperty = DependencyProperty.Register("Top", typeof(double), typeof(SmartCanvas), new PropertyMetadata(0.0));
            LeftProperty = DependencyProperty.Register("Left", typeof(double), typeof(SmartCanvas), new PropertyMetadata(0.0));
            BottomProperty = DependencyProperty.Register("Bottom", typeof(double), typeof(SmartCanvas), new PropertyMetadata(0.0));
            RightProperty = DependencyProperty.Register("Right", typeof(double), typeof(SmartCanvas), new PropertyMetadata(0.0));
        }

        public double Top
        {
            get { return (double)base.GetValue(TopProperty); }
            set { base.SetValue(TopProperty, value); }
        }

        public double Bottom
        {
            get { return (double)base.GetValue(BottomProperty); }
            set { base.SetValue(BottomProperty, value); }
        }

        public double Left
        {
            get { return (double)base.GetValue(LeftProperty); }
            set { base.SetValue(LeftProperty, value); }
        }

        public double Right
        {
            get { return (double)base.GetValue(RightProperty); }
            set { base.SetValue(RightProperty, value); }
        }

       private double GetTop(UIElement element)
        {
            return (double)this.GetValue(TopProperty);
        }

        private double GetLeft(UIElement element)
        {
            return (double)this.GetValue(LeftProperty);
        }

        private double GetBottom(UIElement element)
        {
            return (double)this.GetValue(BottomProperty);
        }

        private double GetRight(UIElement element)
        {
            return (double)this.GetValue(RightProperty);
        }

        protected override Size ArrangeOverride(Size arrangeSize)
        {
            Point middle = new Point(arrangeSize.Width / 2, arrangeSize.Height / 2);

            foreach (UIElement element in base.InternalChildren)
            {
                if (element == null)
                {
                    continue;
                }
                double x = 0.0;
                double y = 0.0;
                double left = GetLeft(element);
                if (!double.IsNaN(left))
                {
                    x = left;
                }

                double top = GetTop(element);
                if (!double.IsNaN(top))
                {
                    y = top;
                }

                element.Arrange(new Rect(new Point(middle.X + x, middle.Y + y), element.DesiredSize));
            }
            return arrangeSize;
        }
    }
}

I'm trying to write a panel class from this two resources:

The panel class will have two attached properties "X" and "Y" and if any element gives x and y to be zero then it will be placed on the center of the Panel. The panel will also let the user to drag things around . Please help me write this class. I'm very new to WPF.


This is how far I've come. Now I tried to implement this but its not working, if you can help me implement the GetTop,GetLeft,GetBottom,GetRight functions which are not by default defined in panel class and which are neccessary. If these 4 methods are present then dragging functions can be implemented here.

using System;
using System.Linq;
using System.Windows;
using System.ComponentModel;
using System.Windows.Controls;
using System.Windows.Media;

namespace SmartERP.Elements
{
    public class SmartCanvas : Panel
    {
        public static readonly DependencyProperty TopProperty;
        public static readonly DependencyProperty LeftProperty;
        public static readonly DependencyProperty BottomProperty;
        public static readonly DependencyProperty RightProperty;

        static SmartCanvas()
        {
            TopProperty = DependencyProperty.Register("Top", typeof(double), typeof(SmartCanvas), new PropertyMetadata(0.0));
            LeftProperty = DependencyProperty.Register("Left", typeof(double), typeof(SmartCanvas), new PropertyMetadata(0.0));
            BottomProperty = DependencyProperty.Register("Bottom", typeof(double), typeof(SmartCanvas), new PropertyMetadata(0.0));
            RightProperty = DependencyProperty.Register("Right", typeof(double), typeof(SmartCanvas), new PropertyMetadata(0.0));
        }

        public double Top
        {
            get { return (double)base.GetValue(TopProperty); }
            set { base.SetValue(TopProperty, value); }
        }

        public double Bottom
        {
            get { return (double)base.GetValue(BottomProperty); }
            set { base.SetValue(BottomProperty, value); }
        }

        public double Left
        {
            get { return (double)base.GetValue(LeftProperty); }
            set { base.SetValue(LeftProperty, value); }
        }

        public double Right
        {
            get { return (double)base.GetValue(RightProperty); }
            set { base.SetValue(RightProperty, value); }
        }

       private double GetTop(UIElement element)
        {
            return (double)this.GetValue(TopProperty);
        }

        private double GetLeft(UIElement element)
        {
            return (double)this.GetValue(LeftProperty);
        }

        private double GetBottom(UIElement element)
        {
            return (double)this.GetValue(BottomProperty);
        }

        private double GetRight(UIElement element)
        {
            return (double)this.GetValue(RightProperty);
        }

        protected override Size ArrangeOverride(Size arrangeSize)
        {
            Point middle = new Point(arrangeSize.Width / 2, arrangeSize.Height / 2);

            foreach (UIElement element in base.InternalChildren)
            {
                if (element == null)
                {
                    continue;
                }
                double x = 0.0;
                double y = 0.0;
                double left = GetLeft(element);
                if (!double.IsNaN(left))
                {
                    x = left;
                }

                double top = GetTop(element);
                if (!double.IsNaN(top))
                {
                    y = top;
                }

                element.Arrange(new Rect(new Point(middle.X + x, middle.Y + y), element.DesiredSize));
            }
            return arrangeSize;
        }
    }
}

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

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

发布评论

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

评论(1

岁月苍老的讽刺 2024-08-23 09:52:38

面板类将有两个附加属性“X”和“Y”[...]

好的,那么您应该实现这些附加属性。请参阅附加的自定义附加属性部分中的示例MSDN 上的属性概述X 的外观如下:

public static readonly DependencyProperty XProperty =
    DependencyProperty.RegisterAttached("X", typeof(double),
        typeof(SmartCanvas), new FrameworkPropertyMetadata(0.0));

public static void SetX(UIElement element, double value) { element.SetValue(XProperty, value); }
public static double GetX(UIElement element) { return (double)element.GetValue(XProperty); }

完成此操作后,您将获得 GetXGetY,这可能就是您所说的 >GetTopGetLeft

The panel class will have two attached properties "X" and "Y" [...]

OK, then you should implement those attached properties. See the example in Section Custom Attached Properties of the Attached Properties Overview on MSDN. Here's how this would look for X:

public static readonly DependencyProperty XProperty =
    DependencyProperty.RegisterAttached("X", typeof(double),
        typeof(SmartCanvas), new FrameworkPropertyMetadata(0.0));

public static void SetX(UIElement element, double value) { element.SetValue(XProperty, value); }
public static double GetX(UIElement element) { return (double)element.GetValue(XProperty); }

Once you have done this, you have GetX and GetY, which is probably what you mean by GetTop and GetLeft.

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