简体   繁体   English

简单的用户控制和MVVM模式:如何使用?

[英]Simple usercontrol and MVVM pattern: How to use?

Please help me 请帮我

I have 我有

public partial class OrderControl : UserControl
{
    private OrderHeader orderHeader;
    public Customer selectedCustomer { get; set; }
    private Customer[] allCustomers;
    public User selectedManager { get; set; }
    private User[] allManagers;


    public OrderControl()
    {
        InitializeComponent();
        DataContext = this;
    }
...
}

And I need one way binding to source: 我需要一种绑定到源的方式:

<ComboBox Name="CustomerComboBox" SelectedItem="{Binding selectedCustomer}"/>

Is this best way to keep selectedCustomer Property in OrderControl.xaml.cs or I need to create some OrderViewModel class with ..,selectedCustomer,... Properties and keep an instance of OrderViewModel in OrderControl.xaml.cs? 这是将selectedCustomer属性保留在OrderControl.xaml.cs中的最佳方法,还是我需要使用..,selectedCustomer,...属性创建一些OrderViewModel类,并在OrderControl.xaml.cs中保留OrderViewModel的实例?

thanks 谢谢

It is best to create a ViewModel class, move your properties to that class and make it a DataContext of your UserControl . 最好创建一个ViewModel类,将属性移至该类,并使其成为UserControlDataContext

Also, your selectedCustomer property is just a regular .NET property and it needs to support INotifyPropertyChanged interface in order to facilitate binding and change notification... typically a base ViewModel class from which all your other ViewModel classes inherit would implement this interface... 此外,您的selectedCustomer属性只是一个常规的.NET属性,它需要支持INotifyPropertyChanged接口以促进绑定和更改通知...通常是所有其他ViewModel类都继承自其的基础ViewModel类将实现此接口...

That will work if you implement INotifyPropertyChanged. 如果实现INotifyPropertyChanged,那将起作用。 Right now there is no way for the combobox to get updates when the property is set. 现在,设置属性后,组合框无法获取更新。 See http://msdn.microsoft.com/en-us/library/ms229614.aspx 请参阅http://msdn.microsoft.com/en-us/library/ms229614.aspx

However, if you wish to follow MVVM, then you will want to create a view model object. 但是,如果您希望遵循MVVM,则将需要创建一个视图模型对象。

if you wanna create real usercontrols you should not: 如果要创建真实的用户控件,则不应:

 DataContext = this;

here a quote from HB 这是HB的报价

It's bad practice, setting the DataContext like that is invisible "from the outside" and impractical as inheritance of the DataContext is usually what you want and expect 这是一种不好的做法,将DataContext设置为“从外部”是不可见的,并且不切实际,因为通常需要和期望对DataContext的继承

here is are similar question and answer. 是类似的问答。

but if you wanna do MVVM with viewmodel first. 但是,如果您想先使用ViewModel做MVVM。

quote from Rachel: 引用雷切尔的话:

Remember, with MVVM your ViewModels are your application. 记住,使用MVVM,您的ViewModels是您的应用程序。 The View is just a pretty interface that allows users to interact with your ViewModels. 视图只是一个漂亮的界面,允许用户与您的ViewModels进行交互。

that mean you should create appropriate viewmodels with all properties and commands you need. 这意味着您应该使用所需的所有属性和命令来创建适当的视图模型。 remove all code from your usercontrol because its now just a view. 从用户控件中删除所有代码,因为它现在只是一个视图。 viewmodel first connects the viewmodel and the view through datatemplates. viewmodel首先通过数据模板连接viewmodel和视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM