thecoop
thecoop

Reputation: 46098

Create dependency property without inheriting off DependencyObject

I've added a basic auto-property to a class, and I want that property to be set whenever the SelectedItem of a treeview changes. So I add this to a Resources somewhere in my XAML:

<myns:MyClass x:Key="MyClassResource" MyProperty="{Binding ElementName=treeView, Path=SelectedItem, Mode=OneWay}" />

but when I try and run this, I get an exception: 'A Binding can only be set on a dependency property'. Now, dependency properties require inheriting off DependencyObject, but MyClass already inherits a class! I don't need all the two-way binding stuff, all I want is that whenever SelectedItem changes, the value gets copied into MyProperty.

How can I do this in a simple way?

Upvotes: 1

Views: 948

Answers (1)

ColinE
ColinE

Reputation: 70122

You can make MyProperty a regular CLR property, the define your binding on your treeView's SelectedItemproperty, setting the binding mode to OneWayToSource, this will cause it to 'push' changes to your MyProperty property on your class.

Upvotes: 1

Related Questions