Andyz Smith February 2016

WPF Dependency Object

Has anyone ever heard of implementing IDependencyObject instead of inheriting from it -- that way one could actually create a class hierarchy instead of having to use only interfaces when trying to get both dependency object/property and custom behavior on our classes.

I want to have a hierarchy of class kinds that are directly usable in the context of an existing structure, i.e. Polygon. I want to be able to use my PolyType in any place, and without any more dialogue and indirection that would be required if I place the PolyGon existing type as a Part of my DependencyObject. But I also want to be able to have my class as the a) the target of {Binding} markup extension, b) Animate properties of PolyType and c) apply themed styling to PolyType.

I want to implement IDependencyObject instead of being forced to inherit from it directly, and obstructing my ability to be a direct descendent and usable in place of, PolyGon.

Answers


Kelly Barnard February 2016

Not sure why you have to inherit from DependencyObject. I use a custom code snippet that generates the following code to register a dependancy property:

public partial class UserControl1 : UserControl
{
public static DependencyProperty MyPropertyProperty = DependencyProperty.Register("MyProperty", typeof(Polygon), typeof(UserControl1), new FrameworkPropertyMetadata(new PropertyChangedCallback(MyProperty_Changed)));

public Polygon MyProperty
{
    get { return (Polygon)GetValue(MyPropertyProperty); }
    set { SetValue(MyPropertyProperty, value); }
}


private static void MyProperty_Changed(DependencyObject o, DependencyPropertyChangedEventArgs args)
{
    UserControl1 thisClass = (UserControl1)o;
    thisClass.SetMyProperty();
}

private void SetMyProperty()
{
    //Put Instance MyProperty Property Changed code here
}


public UserControl1()
{
    InitializeComponent();
}

}

As you can see the DependencyObject can be any type of object. If this is not what you need, please post you code examples, or explain your situation better.

Post Status

Asked in February 2016
Viewed 2,577 times
Voted 7
Answered 1 times

Search




Leave an answer