【发布时间】:2011-07-03 17:51:44
【问题描述】:
我知道多重继承已经过时了,但是有没有办法为 System.Windows.Point 创建一个可以从它继承但仍然实现可绑定依赖属性的包装器?
我正在尝试编写代码,以便为我的 XAML 创建如下语句而不会出现问题:
<custom:Point X="{Binding Width, ElementName=ParentControlName}" Y="{Binding Height, ElementName=ParentControlName}" />
这将使像多边形、路径、线段和其他控件这样的编码变得更加容易。
以下代码是一厢情愿的想法,我知道它绝不会起作用,但这是我希望能够做的事情:
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;
}
}
}
【问题讨论】:
标签: c# wpf inheritance dependency-properties