我正在尝试从这两个资源中编写一个面板类:
面板类将有两个附加属性“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;
}
}
}