ViewModel应该继承WPF中的DependencyObject吗?

问题描述 投票:3回答:1

我试图使用MVVM在WPF中创建一个简单的UserControl。现在,我需要为UserControl创建一个依赖项属性,因此我尝试在UserControlViewModel中创建依赖项属性(我不想被隐藏在代码的后面)。

为了在UserControlViewModel中创建依赖项属性,我需要从DependencyObject继承。在DependencyObject中继承UserControlViewModel是一种好习惯吗?也就是说,这是遵循MVVM设计UserControl的好方法吗?

c# wpf mvvm user-controls dependency-properties
1个回答
8
投票

如果您创建的自定义控件具有希望它们为[[可绑定的属性(例如,以下代码),则不能使用INotifyPropertyChanged,并且必须使用[C0 ]。

DependencyObject
但是使用<MyUserControl MyDependencyProperty="{Binding PropertyPath}"  />
时应记住:

    DependencyObjects未标记为可序列化。
  1. DependencyObjects类覆盖并密封DependencyObjectEquals()方法。
  2. A GetHashCode()具有线程相似性-它只能在创建它的线程上访问。

    • 要查看一个很好的MVVM示例,该示例讨论了View-Model中INPC和DP的实现,请参见此DependencyObject
    • 有关INPC与DP辩论的更多信息,请阅读article
© www.soinside.com 2019 - 2024. All rights reserved.