MVVM 用户控件 - 我在哪里声明它以从页面获取数据?
我有一个 WPF 用户控件...它位于 MVVM 中。用户控件(包含列表视图)需要来自页面(包含它的位置)的数据。我必须在视图后面的代码中设置一个属性才能获取此数据输入。这是否符合MVVM(但据我所知,MVVM模式不支持在视图文件后面的代码中添加代码)。如果不符合,那么有什么办法呢?
I have a WPF user control ...which is in MVVM. The user control(which contains a listview) need data from the page (where it is included). I have to set a property in View's code behind to get this data input. Will this comply with MVVM(But MVVM pattern do not support adding code in code behind file of view as far as i know).if not, what is the way for the same?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您想通过数据绑定来做到这一点。这些控件绑定到视图模型中的属性,视图模型接收数据,应用所需的逻辑并将其返回给视图以进行显示。
请查看此处,了解所有这些工作原理。
You want to do this via data binding. The controls are bound to properties in your viewmodel which receives the data, applies the needed logic and gives it back to the view for displaying it.
Have a look here to get an idea on how all that works.
我有一个链接: http: //social.msdn.microsoft.com/Forums/en/wpf/thread/a3eedc3e-0d59-420c-aba0-44fe8b00552f
但我并没有真正理解注入的含义(如下所示):
名为 IUserControlModel 的 UserControl 公共模型的接口。它具有从外部可见的属性;
- UserControlViewModel,其中包含注入到构造函数中的 IUserControlModel 类型的公共属性;加上用于特定于用户控件实现的 XAML 绑定的所有属性; XAML 也可能直接绑定到 IUserControlModel 属性;
- 一个 MainWindowViewModel,其中嵌套了 IUserControlModel。
I have got a link : http://social.msdn.microsoft.com/Forums/en/wpf/thread/a3eedc3e-0d59-420c-aba0-44fe8b00552f
But I'm not really getting whats meant by injection in it (as given below) :
an interface to the UserControl public model called IUserControlModel. It has the properties that should be visible from outside;
- a UserControlViewModel that contains a public property of type IUserControlModel that is injected in the constructor; plus all the properties used for XAML binds specific to the usercontrol implementation; XAML may have binds directly to the IUserControlModel properties too;
- a MainWindowViewModel that nests the IUserControlModel inside.
我认为你的问题可以用更简单的方式解决。如果将 ListView 的 ItemsSource 属性公开为用户控件的依赖属性,则可以实现您想要的目标,而无需实现 MVVM 模式的不必要的(在本例中)开销:然后您可以使用数据绑定从用户控件所在的页面添加数据包括。
我认为可以回答您的问题的帖子:
发布链接
I think your problem can be solved in easier way. If you expose ItemsSource property of ListView as Dependency Property of your user control you can achieve what you want without unnecesary (in this case) overhead of implementing MVVM pattern : You then can just use databinding to add data from the page where the user control is included.
post that I think answers your question :
Post link