3

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.

Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
Alexander Knoth
  • 137
  • 1
  • 13

1 Answers1

2

The point is that XY1 and XY2 are dependency properties in the first version and simple .NET properties in the second. See Why dependency properties?

In general, AddOwner is used to make a dependency property available on a type that does not ultimately derive from the type that owns the dependency property.

Community
  • 1
  • 1
Jon
  • 428,835
  • 81
  • 738
  • 806
  • Thank you for clarification and for the link you provided. Ok, in common I have understood what does AddOwner mean, but the thing is in some cases I can replace it by using delegates or reflection. In my mind AddOwner just save memory resources, keeping the property variable in storage. May be some one provide me a sample which reveals the advantages of AddOwner? – Alexander Knoth May 25 '12 at 19:02