且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

具有MVVM和依赖项属性的WPF用户控件地狱

更新时间:2022-06-16 02:23:57

首先,如果您正在开发将由其他用户使用的UserControl,我认为MVVM不是一个不错的选择.一个无表情的控件是您真正应该开发的.耶利米·莫里尔(Jeremiah Morrill)具有

First off, I don't think MVVM is a good choice if you are developing a UserControl that will be consumed by others. A lookless control is what you really should be developing. Jeremiah Morrill has a blog post about this subject.

话虽如此,如果您具有默认的公共构造函数,则可以使用XAML设置数据上下文.

With that said, you can set the datacontext with XAML if you have a default public constructor.

在ControlView.xaml内部放置:

Inside ControlView.xaml put:

<UserControl.DataContext>
    <local:ControlViewModel />
</UserControl.DataContext>