我尝试使用MVVM在WPF中创建一个简单的UserControl。现在我需要为UserControl创建一个依赖项属性,所以我尝试在UserControlViewModel中创建依赖项属性(我不想在代码隐藏中)。
为了在UserControlViewModel中创建依赖项属性,我需要从DependencyObject继承。在UserControlViewModel中继承DependencyObject是一个好习惯吗?也就是说,它是一种跟踪MVVM设计UserControl的好方法吗?
答案 0 :(得分:7)
如果您创建了一个自定义控件,其中包含您希望它们可绑定的属性(例如,以下代码),那么 不能使用INotifyPropertyChanged
必须使用DependencyObject
。
<MyUserControl MyDependencyProperty="{Binding PropertyPath}" />
但是在使用DependencyObjects
时,您应该记住:
DependencyObjects
未标记为可序列化。 DependencyObject
类会覆盖并密封Equals()
和GetHashCode()
DependencyObject
方法。 try ( //
GzipCompressorInputStream gcis = //
new GzipCompressorInputStream( //
new FileInputStream("a_gunzipped_file.gz") //
) //
) {
String filename = gcis.getMetaData().getFilename();
}
具有线程亲和力 - 它
只能在创建它的线程上访问。