开发者

Create dependency property without inheriting off DependencyObject

开发者 https://www.devze.com 2023-01-31 16:11 出处:网络
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:

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, Mod开发者_运维百科e=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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号