1

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

面板类将有两个附加属性“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;
        }
    }
}
4

1 回答 1

1

面板类将有两个附加属性“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); }

一旦你这样做了,你就有了GetXand GetY,这可能就是你所说的GetTopand GetLeft

于 2009-12-27T22:25:05.250 回答