如何将ViewModel(从自己的ViewModel获取数据)添加到我的ObservableCollection和View?

时间:2019-02-13 20:41:17

标签: c# wpf mvvm

我想检查Excel文件中的部件(workbookPart,worksheetPart等)和共享字符串表索引。

我认为我必须使用方法和类,但是我对如何将它们组合在一起一无所知。

MainViewModel为ValidateVM的创建一个ObservableCollection

namespace ExcelViewModel
{
  public class MainViewModel : ViewModel
  {
    public ICommand ValidateCommand { get; private set; }    

    public MainViewModel()
    {
      ObservableCollection<ValidateViewModel> ObservCol = new ObservableCollection<ValidateViewModel>();

      ValidateCommand = new CommandBase(PerformValidationExecute);
    }

    private ObservableCollection<ValidateViewModel> _listofValidateViewModels;
    public ObservableCollection<ValidateViewModel> ListOfValidateViewModels
    {
      get
      {
        return _listofValidateViewModels;
      }
      set
      {
        if (_listofValidateViewModels == value)
          return;
        _listofValidateViewModels = value;
        RaisePropertyChanged("ListOfValidateViewModels");       
      }
    }
    void PerformValidationExecute(object param)
    {
      if (param is ObservableCollection<ValidateViewModel>)
        PerformValidation(param as ObservableCollection<ValidateViewModel>);
      else
        throw new InvalidOperationException("TODO!");
    }

    void PerformValidation(ObservableCollection<ValidateViewModel> ObservCol)
        {
            foreach (ValidateViewModel VWM in ListOfValidateViewModels)
            {
                ObservCol.Add(new PartCountVM());
                ObservCol.Add(new SharedStringVM());
            } 
        }
  }
}

ValidateVM是抽象的,具有“名称”和“状态”属性,并打开工作簿。没有其他方法如何打开工作簿?

namespace ExcelViewModel
{
  public abstract class ViewModel : INotifyPropertyChanged
  {
    protected ViewModel()
    {

    }
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void RaisePropertyChanged(string propertyName)
    {
      if( PropertyChanged != null)
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
  }
  public abstract class ValidateViewModel : ViewModel
  {
    protected ValidateViewModel(string name)
    {
        this.Name = name;
        Status = ValidationStatus.notReady;
    }

    private string _name;
    public string Name
    {
      get
      {
        return _name;
      }
      set
      {      
        if (_name == value)
          return;
        _name = value;
        RaisePropertyChanged("Name");     
      }
    }
    public enum ValidationStatus { notReady, OK, failed };
    private ValidationStatus _status;
    public ValidationStatus Status
    {
      get
      {
        return _status;
      }
      set
      {
        if (_status == value)
          return;
        _status = value;
        RaisePropertyChanged("Status");
      }
    }

    public abstract void performValidationCheck();

    private SpreadsheetDocument _document;
    private WorkbookPart _workbookPart;
    private Workbook _workbook;
    private WorkbookStylesPart _workbookStylesPart;
    private Stylesheet _styleSheet;
    private SharedStringTablePart _sstPart;
    private Sheets _sheets;
    public SpreadsheetDocument Document
    {
      get
      {
        return _document;                        
      }
      set
      {
        if (value == null)
          _document = SpreadsheetDocument.Open(path, false);     
      }
    }
    public WorkbookPart WorkbookPart
    {
      get
      {
        return _workbookPart;
      }
      set
      {
        if (value == null)
          _workbookPart = Document.WorkbookPart;
      }
    }
    public Workbook Workbook
    {
      get
      {
        return _workbook;
      }
      set
      {
        if (value == null)
          _workbook = WorkbookPart.Workbook;
      }
    }
    public WorkbookStylesPart WorkbookStylesPart
    {
      get
      {
        return _workbookStylesPart;
      }
      set
      {
        if (value == null)
          _workbookStylesPart = WorkbookPart.WorkbookStylesPart;
      }
    }
    public Stylesheet StyleSheet
    {
      get
      {
        return _styleSheet;
      }
      set
      {
        if (value == null)
          _styleSheet = WorkbookStylesPart.Stylesheet;
      }
    }
    public SharedStringTablePart SstPart
    {
      get
      {
        return _sstPart;
      }
      set
      {
        if (value == null)
          _sstPart = WorkbookPart.SharedStringTablePart;
      }
    }
    public Sheets Sheets
    {
      get
      {
        return _sheets;
      }
      set
      {
        if (value == null)
          _sheets = Workbook.Sheets;
      }
    }
    }
}

PartCountVM和SharedStringVM继承ValidateVM。

namespace ExcelViewModel
{
    class PartCountVM : ValidateViewModel
    {
        public PartCountVM() : base("PartCountVM")
        {}

        public override void performValidationCheck()
        {                      

                int worksheetpartCount = 0;
                int sheetDataCount = 0;
                int sheetsCount = 0;

                foreach (WorksheetPart wsp in WorkbookPart.WorksheetParts)
                {
                    worksheetpartCount++;
                    SheetData[] sheetDataElements = wsp.Worksheet.Elements<SheetData>().ToArray();
                    sheetDataCount += sheetDataElements.Length;
                }


                Name = "Number of WorksheetPart objects: " + worksheetpartCount;
                Name = "Number of WorksheetPart objects: " + worksheetpartCount;
                Name = "Number of Sheetdata objects: " + sheetDataCount;
                Name = "Number of Sheets objects: " + Sheets.Count();


                sheetsCount = Sheets.Count();

                if (worksheetpartCount == sheetDataCount && sheetDataCount == sheetsCount)
                {
                    Name = "Number of parts is correct.";
                }
                else
                {
                    Name = "ERROR: Number of parts is not correct. Please make sure every WorksheetPart has it's own SheetData and Sheets.";
                }


                }
            }
        }
namespace ExcelViewModel
{
  class SharedStringVM : ValidateViewModel
  {
    public SharedStringVM() : base("notReady")
    {    }

    public override void performValidationCheck()
    {

      foreach (WorksheetPart wsp in WorkbookPart.WorksheetParts)
      {
        SheetData sheetData = wsp.Worksheet.Elements<SheetData>().First();

        int index;
        int sstLength = 0;

        foreach (SharedStringItem ssItem in SstPart.SharedStringTable.Elements<SharedStringItem>())
        {
          sstLength++;
        }

        foreach (Row row in sheetData.Elements<Row>())
        {

          foreach (Cell cell in row.Elements<Cell>())
          {
            if (cell.DataType.Value == CellValues.SharedString )
            {
              bool success = int.TryParse(cell.CellValue.InnerText, out index);

              if (success == true)
                if (index > sstLength) Name = "Error: Index is not in Shared String Table";             
            }
          }
        }
      }
    }
  }
}

我为可能充满错误的凌乱代码感到抱歉,但是我对如何继续此处迷失了。

最终,我希望将来自PartcountVM和SharedStringVM中的数据添加到单个ValidateVM中,这些都添加到ObvservableCollection中,然后将其显示在视图的Datagrid中。

0 个答案:

没有答案