255 lines
6.7 KiB
C#
255 lines
6.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.ComponentModel;
|
|
using System.Linq;
|
|
using System.Windows;
|
|
using BS.Shared.Extensions;
|
|
|
|
namespace BeWo.ViewModel.ListViewModel
|
|
{
|
|
public abstract class AbstractDCListMapperVM<DCType, VMType> : AbstractBaseVM where VMType : AbstractDCMapperVM<DCType> where DCType : new()
|
|
{
|
|
|
|
public static string PropertyName_EditVM = "EditVM";
|
|
|
|
public static string PropertyName_NewVM = "NewVM";
|
|
|
|
private readonly ObservableCollection<DCType> _DCBackup;
|
|
|
|
private VMType _EditVM;
|
|
|
|
private VMType _NewVM;
|
|
|
|
private VMType _SelectedVM;
|
|
|
|
private BindingList<VMType> _VMList;
|
|
|
|
//public AbstractDCListMapperVM(IEnumerable<AbstractDCListMapperVM<DCType, VMType>> listVMs, bool ignore)
|
|
//{
|
|
// var dcLists = listVMs.SelectMany(x => x.DCBackup);
|
|
|
|
// _DCBackup = dcLists != null ? dcLists.ToObservableCollection() : new ObservableCollection<DCType>();
|
|
|
|
// _DCBackup.CollectionChanged += (s, e) => _VMList.AddRange(e.NewItems.Cast<DCType>().Select(CreateVM));
|
|
|
|
// var vmLists = listVMs.SelectMany(x => x.VMList).ToList();
|
|
|
|
// _VMList = new BindingList<VMType>(vmLists);
|
|
//}
|
|
|
|
public AbstractDCListMapperVM(IEnumerable<DCType> pDCList)
|
|
{
|
|
_DCBackup = pDCList != null ? pDCList.ToObservableCollection() : new ObservableCollection<DCType>();
|
|
|
|
_DCBackup.CollectionChanged += (s, e) => _VMList.AddRange(e.NewItems.Cast<DCType>().Select(CreateVM));
|
|
}
|
|
|
|
public AbstractDCListMapperVM() : this(null) {}
|
|
|
|
|
|
public virtual int AddedCount
|
|
{
|
|
get
|
|
{
|
|
return this.VMList.Where(vm => vm.IsNew).Count();
|
|
}
|
|
}
|
|
|
|
public virtual int ChangedCount
|
|
{
|
|
get
|
|
{
|
|
return this.VMList.Where(vm => vm.IsDirty && !vm.IsNew).Count();
|
|
}
|
|
}
|
|
|
|
public ObservableCollection<DCType> DCBackup
|
|
{
|
|
get
|
|
{
|
|
return this._DCBackup;
|
|
}
|
|
}
|
|
|
|
public VMType EditVM
|
|
{
|
|
get
|
|
{
|
|
return _EditVM;
|
|
}
|
|
|
|
set
|
|
{
|
|
_EditVM = value;
|
|
FirePropertyChanged(PropertyName_EditVM);
|
|
}
|
|
}
|
|
|
|
public VMType SelectedVM
|
|
{
|
|
get => _SelectedVM;
|
|
set
|
|
{
|
|
_SelectedVM = value;
|
|
FirePropertyChanged(nameof(SelectedVM));
|
|
}
|
|
}
|
|
|
|
public override bool IsDirty
|
|
{
|
|
get
|
|
{
|
|
return ChangedCount != 0 || RemovedCount != 0 || AddedCount != 0;
|
|
}
|
|
}
|
|
|
|
public VMType NewVM
|
|
{
|
|
get
|
|
{
|
|
return _NewVM ?? (NewVM = CreateVM(new DCType()));
|
|
}
|
|
|
|
set
|
|
{
|
|
_NewVM = value;
|
|
FirePropertyChanged(PropertyName_NewVM);
|
|
}
|
|
}
|
|
|
|
public virtual int RemovedCount
|
|
{
|
|
get
|
|
{
|
|
return GetRemoved().Count;
|
|
}
|
|
}
|
|
|
|
public BindingList<VMType> VMList
|
|
{
|
|
get
|
|
{
|
|
if (_VMList == null)
|
|
{
|
|
_VMList = new BindingList<VMType>(_DCBackup.Select(CreateVM).ToList()) {AllowNew = true};
|
|
_VMList.AddingNew += (s, e) => e.NewObject = CreateVM(new DCType());
|
|
}
|
|
|
|
return _VMList;
|
|
}
|
|
|
|
set
|
|
{
|
|
_VMList = value;
|
|
}
|
|
}
|
|
|
|
protected virtual Comparison<VMType> Comparison
|
|
{
|
|
get
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public virtual VMType AddNewVMToList()
|
|
{
|
|
VMList.Add(NewVM);
|
|
|
|
// VMList.Sort();
|
|
VMType lVM = NewVM;
|
|
NewVM = null;
|
|
|
|
//FirePropertyChanged("VMList");
|
|
return lVM;
|
|
}
|
|
|
|
public VMType AddNewVMToList(bool pSetAsEdit)
|
|
{
|
|
_VMList.Add(_NewVM);
|
|
|
|
// _VMList.Sort();
|
|
if (pSetAsEdit)
|
|
{
|
|
EditVM = _NewVM;
|
|
}
|
|
|
|
NewVM = null;
|
|
return EditVM;
|
|
}
|
|
|
|
public VMType AddNewVMToListAndSelect(bool auto_select)
|
|
{
|
|
// Wichtig! NewVM, wenn _NewVM null ist
|
|
_VMList.Add(NewVM);
|
|
|
|
// _VMList.Sort();
|
|
if (auto_select)
|
|
{
|
|
SelectedVM = _NewVM;
|
|
}
|
|
|
|
FirePropertyChanged(nameof(VMList));
|
|
|
|
NewVM = null;
|
|
return SelectedVM;
|
|
}
|
|
|
|
public virtual List<DCType> CommitAdded()
|
|
{
|
|
return this.VMList.Where(vm => vm.IsNew).Select(vm => vm.CommitToDataContract()).ToList();
|
|
}
|
|
|
|
public virtual List<DCType> CommitChanged()
|
|
{
|
|
return this.VMList.Where(vm => vm.IsDirty && !vm.IsNew).Select(vm => vm.CommitToDataContract()).ToList();
|
|
}
|
|
|
|
public virtual List<DCType> CopyToDCList(bool doCommit)
|
|
{
|
|
if (doCommit && this._VMList != null)
|
|
{
|
|
return doCommit
|
|
? this._VMList.Select(vm => vm.CommitToDataContract()).ToList()
|
|
: this._VMList.Select(i => i.CopyToEmptyDataContract()).ToList();
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public void DeleteEditVM()
|
|
{
|
|
this._VMList.Remove(this._EditVM);
|
|
this.EditVM = null;
|
|
}
|
|
|
|
public void DeleteSelectedVM(string displayname = null)
|
|
{
|
|
if (displayname is object)
|
|
{
|
|
var res = MessageBox.Show($"Möchten Sie {displayname} wirklich löschen?", "Löschen bestätigen", MessageBoxButton.YesNo);
|
|
|
|
if (res != MessageBoxResult.Yes)
|
|
return;
|
|
}
|
|
|
|
VMList.Remove(SelectedVM);
|
|
|
|
FirePropertyChanged(nameof(VMList));
|
|
}
|
|
|
|
public virtual List<DCType> GetRemoved()
|
|
{
|
|
return this._DCBackup.Where(dc => !this._VMList.Select(vm => vm.DataContract).Contains(dc)).ToList();
|
|
}
|
|
|
|
|
|
protected virtual VMType CreateVM(DCType pDC)
|
|
{
|
|
var lConstr = typeof(VMType).GetConstructor(new[] { typeof(DCType) });
|
|
|
|
return lConstr.Invoke(new object[] { pDC }) as VMType;
|
|
}
|
|
}
|
|
} |