Any example of Dependency Property in ViewModel?

后端 未结 2 1450
傲寒
傲寒 2021-01-21 19:04

Can someone give example of Dependency Property in ViewModel in WPF passed as datacontext to view. Will this require inheriting from DependencyObject? Lets say I want ListBox

相关标签:
2条回答
  • 2021-01-21 19:31

    In order to define a DependencyProperty in your view model, your view model class must derive from DependencyObject. Otherwise the DependencyProperty won't work right.

    Do you really need the property to be a DependencyPropety? Have you looked into implementing INotifyPropertyChanged instead?

    0 讨论(0)
  • 2021-01-21 19:48

    Although you can implement a ViewModel as a DependencyObject with dependency properties, most people agree that it's better to use a POCO object implementing INotifyPropertyChanged... Have a look at this article by Kent Boogaart for a detailed comparison between the two approaches. There's also a SO question about this

    0 讨论(0)
提交回复
热议问题