WPF可拖动面板类

时间:2009-12-25 17:50:47

标签: wpf canvas center drag

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

面板类将有两个附加属性“X”和“Y”,如果任何元素给出x和y为零,那么它将被放置在Panel的中心。该面板还将让用户拖动东西。请帮我写这堂课。我是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;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

  

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

好的,那么你应该实现那些附加的属性。请参阅MSDN上Attached Properties Overview自定义附加属性部分中的示例。以下是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的含义。