我知道多重继承已经过时了,但是有没有一种方法可以创建一个包装器,使其可以从System.Windows.Point继承,但仍然实现可绑定的依赖属性?
我正在尝试编写代码,以便在我的XAML中可以轻松创建以下语句:
<custom:Point X="{Binding Width, ElementName=ParentControlName}" Y="{Binding Height, ElementName=ParentControlName}" />
这将使编写Polygons、Paths、LineSegments和其他控件变得更加容易。
以下代码只是一种愿望,我知道它根本行不通,但这就是我想要做到的:
public class BindablePoint: DependencyObject, Point
{
public static readonly DependencyProperty XProperty =
DependencyProperty.Register("X", typeof(double), typeof(BindablePoint),
new FrameworkPropertyMetadata(default(double), (sender, e) =>
{
BindablePoint point = sender as BindablePoint;
point.X = (double) e.NewValue;
}));
public static readonly DependencyProperty YProperty =
DependencyProperty.Register("Y", typeof(double), typeof(BindablePoint),
new FrameworkPropertyMetadata(default(double), (sender, e) =>
{
BindablePoint point = sender as BindablePoint;
point.Y = (double)e.NewValue;
}));
public new double X
{
get { return (double)GetValue(XProperty); }
set
{
SetValue(XProperty, value);
base.X = value;
}
}
public new double Y
{
get { return (double)GetValue(YProperty); }
set
{
SetValue(YProperty, value);
base.Y = value;
}
}
}
RotateTransform
有CenterX
和CenterY
属性,而不是一个中心点Point
。如果我想要动画旋转或将其字段链接到其他类和控件,这样做可以更轻松地绑定这些值。他们在那里放弃了Point
,我敢打赌是因为如果他们没有这样做,管理绑定会更加困难。 - Ross Graeber