Could someone explain, please, what are the advantages of using AddOwner method in WPF ( Dependency properties at all)? I have some misunderstand.
For instanse, look at the code below.
public class TestVisual: Shape
{
private LineGeometry line = new LineGeometry();
public static readonly DependencyProperty XY1Property =
LineGeometry.StartPointProperty.AddOwner(
typeof(TestVisual),
new FrameworkPropertyMetadata(new Point(0,0),
FrameworkPropertyMetadataOptions.AffectsMeasure));
public static readonly DependencyProperty XY2Property =
LineGeometry.EndPointProperty.AddOwner(
typeof(TestVisual),
new FrameworkPropertyMetadata(new Point(0, 0),
FrameworkPropertyMetadataOptions.AffectsMeasure));
public Point XY1
{
get { return (Point)GetValue(XY1Property);}
set { SetValue(XY1Property,value); }
}
public Point XY2
{
get { return (Point)GetValue(XY2Property); }
set { SetValue(XY2Property, value); }
}
protected override Geometry DefiningGeometry
{
get
{
line.StartPoint = XY1 ;
line.EndPoint = XY2;
return line;
}
}
}
From the code above as you can see TestVisual class uses AddOwner method for dependency property. Ok, but the same result we can get more easier (I mean we can get a class which allow to adjust of the line visual element by setting the XY1 and XY2 Point):
public class TestVisual: Shape
{
private LineGeometry line = new LineGeometry();
public Point XY1
{
get;
set;
}
public Point XY2
{
get;
set;
}
protected override Geometry DefiningGeometry
{
get
{
line.StartPoint = XY1 ;
line.EndPoint = XY2;
return line;
}
}
}
So whats the main point ? Thanks in advance.
The point is that
XY1andXY2are dependency properties in the first version and simple .NET properties in the second. See Why dependency properties?In general,
AddOwneris used to make a dependency property available on a type that does not ultimately derive from the type that owns the dependency property.