如何将我的模型结合在一起以使其正常工作?

时间:2018-07-09 13:48:53

标签: c# wpf mvvm

我试图找到一种方法可以将我的所有模型以多种不同的方式集成到一个类中,但是没有任何效果,所以这是我的代码有人可以帮忙吗? 目前可以使用,但是我想有更好的做法,因为我是MVVM的新手

此应用正在文本框中输入内容并在文本块中返回

型号:

DateEntry.cs

/// <summary>
/// Entity Class for the Date 
/// </summary>
namespace FumeHood1._0._2.Models
{
    using System;
    using System.ComponentModel;
    public class DateEntry : INotifyPropertyChanged, IDataErrorInfo
    {
        private string _date;

        public DateEntry(string testDate)
        //Initializes a new instance of the LabelDataEntry Class
        {
            Date = testDate;
        }

        //Gets or Sets the Date

        public string Date
        {
            get { return _date; }
            set
            {
                _date = value;
                OnPropertyChanged("Date");
            }
        }


        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion

        #region IDataErrorInfo Members
        public string Error
        {
            get;
            private set;
        }
        public string this[string columnName]
        {
            get
            {
                if (columnName == "Date")
                {
                    if (String.IsNullOrWhiteSpace(Date))
                    {
                        Error = "Name cannot be null or empty";
                    }
                    else
                    {
                        Error = null;
                    }
                }
                return Error;
            }
        }
        #endregion
    }
}

RoomNumberEntry.cs

/// <summary>
/// Entity Class for the Room Numbers
/// </summary>
namespace FumeHood1._0._2.Models
{
    using System;
    using System.ComponentModel;
    public class RoomNumberEntry : INotifyPropertyChanged, IDataErrorInfo
    {
        private string _roomNumber;

        public RoomNumberEntry(string testRoomNumber)
        //Initializes a new instance of the LabelDataEntry Class
        {
            RoomNumber = testRoomNumber;
        }

        //Gets or Sets the Room Number

        public string RoomNumber
        {
            get { return _roomNumber; }
            set
            {
                _roomNumber = value;
                OnPropertyChanged("RoomNumber");
            }
        }


        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion

        #region IDataErrorInfo Members
        public string Error
        {
            get;
            private set;
        }
        public string this[string columnName]
        {
            get
            {
                if (columnName == "RoomNumber")
                {
                    if (String.IsNullOrWhiteSpace(RoomNumber))
                    {
                        Error = "Name cannot be null or empty";
                    }
                    else
                    {
                        Error = null;
                    }
                }
                return Error;
            }
        }
        #endregion

    }
}

ViewModel: MainViewModel.cs

namespace FumeHood1._0._2.ViewModels
{
    using System;
    using FumeHood1._0._2.Models;
    using System.Diagnostics;
    using System.Windows.Input;
    using FumeHood1._0._2.Commands;
    using FumeHood1._0._2.Views;

    internal class MainViewModel
    {
        private DateEntry _labelDataEntry;
        private RoomNumberEntry _roomNumberEntry;
        private ReturnViewModel _childViewModel;


        ///Initializes a new instance of the DateViewModel
        public MainViewModel()
        {
            DateTime today = DateTime.Today;
            _labelDataEntry = new DateEntry(today.ToString("d"));
            _roomNumberEntry = new RoomNumberEntry("5");
            UpdateCommand = new LabelDataEntryCommands(this);

        }



        //Gets the LabelDataEntry instance
        public DateEntry LabelDataEntry
        {
            get { return _labelDataEntry; }
        }

        public RoomNumberEntry RoomNumberEntry
        {
            get { return _roomNumberEntry; }
        }

        //Gets the UpdateCommand for the ViewModel
        public ICommand UpdateCommand
        {
            get;
            private set;
        }

        //Save the changes to the LabelDataEntry
        public void SaveChanges()
        {
            DateInfoView view = new DateInfoView();
            view.DataContext = _childViewModel;

            _childViewModel.Date = LabelDataEntry.Date;
            _childViewModel.RoomNumber = RoomNumberEntry.RoomNumber;
            view.ShowDialog();
        }


    }
}

0 个答案:

没有答案