diff --git a/App/MainWindowViewModel.cs b/App/MainWindowViewModel.cs index 269aea5..6131b3f 100644 --- a/App/MainWindowViewModel.cs +++ b/App/MainWindowViewModel.cs @@ -57,19 +57,19 @@ namespace ModernWpfPlayground public Visibility VisibilityEnumTest { - get => GetProperty(); + get => GetProperty(Visibility.Visible); set => SetProperty(value); } public double SliderTest { - get => GetProperty(100); + get => GetProperty(100D); set => SetProperty(value); } public double ValidationTest { - get => GetProperty(); + get => GetProperty(0D); set => SetProperty(value); } diff --git a/App/ModernWpfPlayground.csproj b/App/ModernWpfPlayground.csproj index 9bd6a89..aea1af3 100644 --- a/App/ModernWpfPlayground.csproj +++ b/App/ModernWpfPlayground.csproj @@ -9,12 +9,12 @@ - - - + + + - + diff --git a/App/MvvmStuff/BaseViewModel.cs b/App/MvvmStuff/BaseViewModel.cs index 309bc92..db3993a 100644 --- a/App/MvvmStuff/BaseViewModel.cs +++ b/App/MvvmStuff/BaseViewModel.cs @@ -16,6 +16,7 @@ namespace ModernWpfPlayground.MvvmStuff private readonly Dictionary _values = new Dictionary(); protected readonly ObjectAccessor ObjectAccessor; + private readonly Dictionary _defaultValues = new Dictionary(); protected IReadOnlyDictionary Values => _values; @@ -34,21 +35,21 @@ namespace ModernWpfPlayground.MvvmStuff protected T GetProperty(T defaultValue = default, [CallerMemberName] string? propertyName = null) { if (propertyName == null) throw new ArgumentNullException(nameof(propertyName)); - return Values.TryGetValue(propertyName, out var obj) ? (T)obj! : defaultValue; + if (Values.TryGetValue(propertyName, out var obj)) + { + return (T) obj!; + } + + _defaultValues[propertyName] =defaultValue; + return defaultValue; } - protected void ResetViewModel(Func? predicate = null) + protected void ResetViewModel() { - IEnumerable keys = _values.Keys.ToArray(); - if (predicate != null) keys = keys.Where(predicate); - foreach (var key in keys) + foreach (var (key, value) in _values.ToArray()) { - var currentValue = ObjectAccessor[key]; - _values.Remove(key); - var newValue = ObjectAccessor[key]; - if (Equals(currentValue, newValue)) continue; - _values.Add(key, currentValue); - ObjectAccessor[key] = newValue; + if (_defaultValues.TryGetValue(key, out var defaultValue) && Equals(value, defaultValue)) continue; + ObjectAccessor[key] = defaultValue; } } @@ -56,14 +57,11 @@ namespace ModernWpfPlayground.MvvmStuff protected void LoadViewModel() { - var keysFromFile = new SortedSet(); + ResetViewModel(); foreach (var (key, value) in GetViewModelItems()) { - keysFromFile.Add(key); ObjectAccessor[key] = value; } - - ResetViewModel(x => !keysFromFile.Contains(x)); } } } \ No newline at end of file diff --git a/Controls/Controls.csproj b/Controls/Controls.csproj index ca94cbd..1a5ff40 100644 --- a/Controls/Controls.csproj +++ b/Controls/Controls.csproj @@ -7,7 +7,7 @@ - +