I know multiple inheritence is out, but is there a way to create a wrapper for System.Windows.Point that can inherit from it but still implement bindable dependency properties?
I'm trying to code so that for my XAML I can create staments like the following without issue:
<custom:Point X="{Binding Width, ElementName=ParentControlName}" Y="{Binding Height, ElementName=ParentControlName}" />
It would make coding things like Polygons, Paths, LineSegments and other controls so much easier.
The following code is provided as wishful thinking and I understand that it will in no way work, but this is the kind of thing I want to be able to do:
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;
}
}
}
You may have to reimplement all the classes (
Point
,Polygon
,Line
etc.) from scratch, becausePoint
is a structure, therefore it doesn't support inheritance (which also explains why Point does not support binding: it cannot inheritDependencyObject
, which contains the necessary infrastructure for DPs).There might be a way though - you could create a subclass of polygon and add a new
DependencyProperty
called "BindablePoints
" which would be an observable collection (you would have to create a custom OC that would fireCollectionChanged
when one of the points changed) of the points. The property would in itsOnChanged
update the mainPoints
property of thePolygon
.I guess this could work, but I'm not sure whether it would be fast enough for whatever you are trying to do. Tou would still have to create subclasses of all the shapes you would want to use, but you wouldn't have to create them all from scratch.
Write a wrapper class for point which implements the INotifyPropertyChanged interface
Bind startpoint / endPoint X and Y properties for example to a line instance
Unfortunately,
Point
is a struct, and structs don't support inheritance. From MSDNMaybe this doesn't answer your question directly but you can easily bind a
Point
with the help of a Converter. In your case it would be likePointConverter
If you just want to bind the X value and have a static Y value you could do this like
PointXConverter
Try to use Converters instead.