摘录一下MVVM相关

1 http://www.codeproject.com/Articles/42548/MVVM-and-the-WPF-DataGrid

http://www.codeproject.com/Articles/36745/Showing-Dialogs-When-Using-the-MVVM-Pattern


2,http://stackoverflow.com/questions/4862709/mvvm-separation-of-data-access-from-viewmodel

ContactListModel.cs

public class ContactListModel//: INotifyPropertyChanged
{
    public int ContactListID { get; set; }
    public string ContactListName { get; set; }
    public ObservableCollection<AggregatedLabelModel> AggLabels { get; set; }
}

InformationViewModel.cs

public class InformationViewModel
{
    public InformationViewModel()
    {
        GetData();
    }

    private ObservableCollection<ContactListModel> myContactLists;

    public IEnumerable<ContactListModel> ContactLists
    {
        get { return myContactLists; }
    }


    public void GetData()
    {

        myContactLists = new ObservableCollection<ContactListModel>();

        DB2Connection conn = null;
        try
        {
            conn = new DB2Connection("SERVER CONNECTION;");
        }
        catch (Exception ex)
        {
            System.Windows.MessageBox.Show(ex.Message + " " + ex.InnerException);
        }

        //get all contactLists and their labels
        DB2Command command = new DB2Command("SELECT QUERY");
        command.Connection = conn;

        conn.Open();

        //Add unique contactLists to dictionary
        Dictionary<int, ContactListModel> myContactDictionary = new Dictionary<int, ContactListModel>();

        using (DB2DataReader dr = command.ExecuteReader())
        {
            while (dr.Read())
            {
                int id = Convert.ToInt32(dr["CONTACT_LIST_ID"]);

                if (!myContactDictionary.ContainsKey(id))
                {

                    ContactListModel contactList = new ContactListModel();

                    contactList.ContactListID = id;
                    contactList.ContactListName = dr["CONTACT_LIST_NAME"].ToString();
                    contactList.AggLabels = new ObservableCollection<AggregatedLabelModel>()
                {
                    new AggregatedLabelModel()
                    {
                        ID = Convert.ToInt32(dr["LABEL_ID"]),
                        Name = dr["LABEL_NAME"].ToString()
                    }

                };
                    myContactDictionary.Add(id, contactList);
                }
                else
                {
                    //populate existing contact lists with remaining labels
                    ContactListModel contactList = myContactDictionary[id];

                    contactList.AggLabels.Add
                    (
                        new AggregatedLabelModel()
                        {
                            ID = Convert.ToInt32(dr["LABEL_ID"]),
                            Name = dr["LABEL_NAME"].ToString()
                        }
                    );
                }
            }
        }
        conn.Close();
}

A rough mock up, you can add SaveContact and DeleteContact methods to the DataLayer class.

    public class DataLayer
    {
        public ObservableCollection<ContactModel> GetContacts()
        {
            var tList = new ObservableCollection<ContactModel>();

            //load from db into tList

            return tList;
        }
    }

    public class ContactModel
    {
        public int ContactListID { get; set; }
        public string ContactListName { get; set; }
        public ObservableCollection<AggregatedLabelModel> AggLabels { get; set; }
    }

    public class ContactsViewModel
    {
        public ObservableCollection<ContactModel> ListOfContacts;

        public ContactsViewModel()
        {
            var dl = new DataLayer();
            ListOfContacts = dl.GetContacts();
        }
    }

3,

class MyViewModel
{
   public MyViewModel(MyModel model)
   {
      this.Name = model.Name;
      this.Colour = model.Colour;
   }

   public string Name {get;set;}
   public string Colour {get;set;}

   // commands here that connect to the following methods

   public void Save()
   {
      model.Name = this.Name;
      model.Colour = this.Colour;
      model.SaveToDatabase();
   }

   public void Cancel()
   {
      this.Name = model.Name;
      this.Colour = model.Colour;
   }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值