diff --git a/MainWindow.xaml b/MainWindow.xaml
index a461991..4bc06a5 100644
--- a/MainWindow.xaml
+++ b/MainWindow.xaml
@@ -8,7 +8,7 @@
xmlns:propertyPresenter2="clr-namespace:ModernWpfPlayground.PropertyPresenter2"
xmlns:ui="http://schemas.modernwpf.com/2019"
x:Name="Window"
- Title="TaBEA 3.0.0"
+ Title="{Binding Title}"
Width="1200"
Height="600"
ui:TitleBar.ExtendViewIntoTitleBar="True"
diff --git a/ModernWpfPlayground.csproj b/ModernWpfPlayground.csproj
index 2ca7435..6d5d014 100644
--- a/ModernWpfPlayground.csproj
+++ b/ModernWpfPlayground.csproj
@@ -8,11 +8,11 @@
-
+
all
runtime; build; native; contentfiles; analyzers; buildtransitive
-
+
diff --git a/MvvmStuff/BaseViewModel.cs b/MvvmStuff/BaseViewModel.cs
index c22fb9f..97b1676 100644
--- a/MvvmStuff/BaseViewModel.cs
+++ b/MvvmStuff/BaseViewModel.cs
@@ -11,16 +11,17 @@ namespace ModernWpfPlayground.MvvmStuff
{
public abstract class BaseViewModel : INotifyPropertyChanged
{
+ private readonly Dictionary _values = new Dictionary();
- private readonly Dictionary _valueDict = new Dictionary();
+ public IReadOnlyDictionary Values => _values;
protected bool SetProperty(T value, Action? onChanged = null,
[CallerMemberName] string? propertyName = null)
{
if(propertyName == null) throw new ArgumentNullException(nameof(propertyName));
- if (_valueDict.TryGetValue(propertyName, out var obj) && Equals(value, obj)) return false;
+ if (_values.TryGetValue(propertyName, out var obj) && Equals(value, obj)) return false;
- _valueDict[propertyName] = value!;
+ _values[propertyName] = value!;
OnPropertyChanged(propertyName);
onChanged?.Invoke(value);
return true;
@@ -29,7 +30,7 @@ namespace ModernWpfPlayground.MvvmStuff
protected T GetProperty(T defaultValue = default, [CallerMemberName] string? propertyName = null)
{
if (propertyName == null) throw new ArgumentNullException(nameof(propertyName));
- return _valueDict.TryGetValue(propertyName, out var obj) ? (T) obj : defaultValue;
+ return Values.TryGetValue(propertyName, out var obj) ? (T) obj : defaultValue;
}
public event PropertyChangedEventHandler? PropertyChanged;
@@ -42,32 +43,22 @@ namespace ModernWpfPlayground.MvvmStuff
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
- protected void SaveViewModel()
- {
- var contents = JsonSerializer.Serialize(_valueDict);
- var saveFileDialog = new SaveFileDialog {AddExtension = true, DefaultExt = "*.json"};
- var result = saveFileDialog.ShowDialog(Application.Current.MainWindow?.Owner);
- if (result == true)
- {
- File.WriteAllText(saveFileDialog.FileName, contents);
- }
- }
-
protected void ResetViewModel()
{
- foreach (var key in _valueDict.Keys)
+ foreach (var key in Values.Keys)
{
- _valueDict.Remove(key);
+ _values.Remove(key);
OnPropertyChanged(key);
}
}
- protected void MapDictionary(IEnumerable<(string, object?)> tuples)
+ protected abstract IEnumerable<(string key, object? value)> GetViewModelItems();
+
+ protected void LoadViewModel()
{
- if (tuples == null) throw new ArgumentNullException(nameof(tuples));
- foreach (var (key, value) in tuples)
+ foreach (var (key, value) in GetViewModelItems())
{
- _valueDict[key] = value!;
+ _values[key] = value!;
OnPropertyChanged(key);
}
}
diff --git a/WindowViewModel.cs b/WindowViewModel.cs
index 007e904..dc02849 100644
--- a/WindowViewModel.cs
+++ b/WindowViewModel.cs
@@ -15,6 +15,31 @@ namespace ModernWpfPlayground
{
private readonly PropertyInfo[] _properties;
private string? _path;
+ private string _title = AppName;
+ private const string AppName = "TaBEA 3.0.0";
+
+ public string? Path
+ {
+ get => _path;
+ private set
+ {
+ if (Equals(_path, value)) return;
+ _path = value;
+ OnPropertyChanged();
+ Title = value != null ? $"{System.IO.Path.GetFileName(value)} - {AppName}" : AppName;
+ }
+ }
+
+ public string Title
+ {
+ get => _title;
+ set
+ {
+ if (Equals(_title, value)) return;
+ _title = value;
+ OnPropertyChanged();
+ }
+ }
public WindowViewModel()
{
@@ -22,7 +47,11 @@ namespace ModernWpfPlayground
CloseCommand = new RelayCommand(x => Application.Current.Shutdown());
OpenViewModelCommand = new RelayCommand(x => LoadViewModel());
SaveViewModelCommand = new RelayCommand(x => SaveViewModel());
- ResetViewModelCommand = new RelayCommand(x => ResetViewModel());
+ ResetViewModelCommand = new RelayCommand(x =>
+ {
+ ResetViewModel();
+ Path = null;
+ });
_properties = GetType().GetProperties();
}
@@ -78,26 +107,6 @@ namespace ModernWpfPlayground
public ICommand ResetViewModelCommand { get; }
- private void LoadViewModel()
- {
- var list = ReadFromJson();
- MapDictionary(list);
- }
-
- private IEnumerable<(string, object?)> ReadFromJson()
- {
- var openFileDialog = new OpenFileDialog { AddExtension = true, DefaultExt = "*.json" };
- var result = openFileDialog.ShowDialog(Application.Current.MainWindow?.Owner);
- if (result != true) yield break;
-
- var contents = File.ReadAllText(_path = openFileDialog.FileName);
- var obj = JsonSerializer.Deserialize>(contents);
- foreach (var (key, value) in obj)
- {
- yield return (key, CastToType(key, value));
- }
- }
-
private object? CastToType(string key, JsonElement value)
{
@@ -106,6 +115,7 @@ namespace ModernWpfPlayground
{
return default;
}
+
if (property.PropertyType == typeof(double))
{
return value.GetDouble();
@@ -133,5 +143,33 @@ namespace ModernWpfPlayground
return default;
}
+
+ private void SaveViewModel()
+ {
+ var contents = JsonSerializer.Serialize(Values);
+ if (Path == null)
+ {
+ var saveFileDialog = new SaveFileDialog {AddExtension = true, DefaultExt = "*.json"};
+ var result = saveFileDialog.ShowDialog(Application.Current.MainWindow?.Owner);
+ if (result != true) return;
+ Path = saveFileDialog.FileName;
+ }
+
+ File.WriteAllText(Path, contents);
+ }
+
+ protected override IEnumerable<(string key, object? value)> GetViewModelItems()
+ {
+ var openFileDialog = new OpenFileDialog {AddExtension = true, DefaultExt = "*.json"};
+ var result = openFileDialog.ShowDialog(Application.Current.MainWindow?.Owner);
+ if (result != true) yield break;
+
+ var contents = File.ReadAllText(Path = openFileDialog.FileName);
+ var obj = JsonSerializer.Deserialize>(contents);
+ foreach (var (key, value) in obj)
+ {
+ yield return (key, CastToType(key, value));
+ }
+ }
}
}
\ No newline at end of file