diff --git a/src/Moryx.Controls.Demo/ViewModels/EntryEditorViewModel.cs b/src/Moryx.Controls.Demo/ViewModels/EntryEditorViewModel.cs index feffc22..71073bf 100644 --- a/src/Moryx.Controls.Demo/ViewModels/EntryEditorViewModel.cs +++ b/src/Moryx.Controls.Demo/ViewModels/EntryEditorViewModel.cs @@ -88,7 +88,8 @@ public EntryEditorViewModel() private void EndEdit(object obj) { - throw new NotImplementedException(); + IsEditMode = false; + EntryViewModels.EndEdit(); } private void CancelEdit(object obj) diff --git a/src/Moryx.Controls/EntryEditor/EntryViewModel.cs b/src/Moryx.Controls/EntryEditor/EntryViewModel.cs index e4abf95..990b612 100644 --- a/src/Moryx.Controls/EntryEditor/EntryViewModel.cs +++ b/src/Moryx.Controls/EntryEditor/EntryViewModel.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; +using System.Collections.Specialized; using System.ComponentModel; using System.Linq; using System.Runtime.CompilerServices; @@ -21,6 +22,7 @@ public class EntryViewModel : INotifyPropertyChanged, IEditableObject #region Fields and Properties private ObservableCollection _subEntries; private string _preEditValue; + private ObservableCollection _preEditSubEntries; /// /// The entry of this view model @@ -62,7 +64,7 @@ public class EntryViewModel : INotifyPropertyChanged, IEditableObject /// public bool IsReadOnly => Entry.Value.IsReadOnly; - // TODO: AL6 Remove direct access to Model and the helper variable _preEditValue from BeginEdit, CancelEdit and EndEdit methods + // TODO: AL6 Remove direct access to Model (insert in CopyFrom/ToModel) and the helper variable _preEditValue from BeginEdit, CancelEdit and EndEdit methods /// /// Current value of /// @@ -90,6 +92,7 @@ public ObservableCollection PossibleValues } } + // TODO: AL6 Remove direct sync to Model (insert in CopyFrom/ToModel) and the helper variable _preEditSubEntries from BeginEdit, CancelEdit and EndEdit methods /// /// Child instances of this /// @@ -98,10 +101,20 @@ public ObservableCollection SubEntries get => _subEntries; set { + if (_subEntries is not null) + _subEntries.CollectionChanged -= SyncSubEntries; _subEntries = value; + _subEntries.CollectionChanged += SyncSubEntries; OnPropertyChanged(); } } + + private void SyncSubEntries(object sender, NotifyCollectionChangedEventArgs args) + { + if (args.Action is NotifyCollectionChangedAction.Move) + return; + Entry.SubEntries = SubEntries.Select(vm => vm.Entry).ToList(); + } #endregion #region Constructors @@ -185,6 +198,7 @@ public void BeginEdit() { SubEntries.BeginEdit(); _preEditValue = Entry.Value.Current; + _preEditSubEntries = new ObservableCollection(SubEntries); } /// @@ -192,6 +206,7 @@ public void EndEdit() { SubEntries.EndEdit(); _preEditValue = Entry.Value.Current; + _preEditSubEntries = new ObservableCollection(SubEntries); CopyToModel(); } @@ -201,7 +216,6 @@ private void CopyToModel() Entry.Value.Current = Value; Entry.Value.Type = ValueType; Entry.Value.UnitType = UnitType; - Entry.SubEntries = SubEntries.Select(vm => vm.Entry).ToList(); UpdateParent(); } @@ -211,6 +225,7 @@ public void CancelEdit() { SubEntries.CancelEdit(); Value = _preEditValue; + Entry.SubEntries = _preEditSubEntries.Select(vm => vm.Entry).ToList(); CopyFromModel(); }