From b910d3a1e6be9e247bfd60684cc831bca4756d24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20B=C3=B6rchers?= Date: Tue, 18 Feb 2020 20:08:06 +0100 Subject: [PATCH] nice features --- BaseViewModel.cs | 25 +++++++++++++++++++++++++ ContentDialogExample.xaml | 14 +++++++++----- ContentDialogExample.xaml.cs | 13 +++++++++++-- MainWindow.xaml | 12 +++--------- RelayCommand.cs | 5 +---- WindowViewModel.cs | 20 +++++++++++++++----- 6 files changed, 64 insertions(+), 25 deletions(-) diff --git a/BaseViewModel.cs b/BaseViewModel.cs index 426e168..1f4038c 100644 --- a/BaseViewModel.cs +++ b/BaseViewModel.cs @@ -1,6 +1,10 @@ using System.Collections.Generic; using System.ComponentModel; +using System.IO; using System.Runtime.CompilerServices; +using System.Text.Json; +using System.Windows; +using Microsoft.Win32; using ModernWpfPlayground.Annotations; namespace ModernWpfPlayground @@ -32,5 +36,26 @@ namespace ModernWpfPlayground PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } + public void SaveViewModel() + { + var contents = JsonSerializer.Serialize(_valueDict); + var saveFileDialog = new SaveFileDialog(); + var result = saveFileDialog.ShowDialog(Application.Current.MainWindow?.Owner); + if (result == true) + { + File.WriteAllText(saveFileDialog.FileName, contents); + } + } + + public void LoadViewModel(string path) + { + var contents = File.ReadAllText(path); + var obj = JsonSerializer.Deserialize>(contents); + foreach (var (key, value) in obj) + { + _valueDict[key] = value; + OnPropertyChanged(key); + } + } } } \ No newline at end of file diff --git a/ContentDialogExample.xaml b/ContentDialogExample.xaml index 0bb70a3..1920763 100644 --- a/ContentDialogExample.xaml +++ b/ContentDialogExample.xaml @@ -3,15 +3,19 @@ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ui="http://schemas.modernwpf.com/2019" - Title="Save your work?" + x:Name="LayoutRoot" + Title="Delete your work?" CloseButtonText="Cancel" DefaultButton="Close" IsShadowEnabled="True" PrimaryButtonText="Yes" SecondaryButtonText="No"> - + - - - + + + diff --git a/ContentDialogExample.xaml.cs b/ContentDialogExample.xaml.cs index 6ca458e..d591439 100644 --- a/ContentDialogExample.xaml.cs +++ b/ContentDialogExample.xaml.cs @@ -1,9 +1,18 @@ -using ModernWpf.Controls; +using System.Windows; namespace ModernWpfPlayground { - public partial class ContentDialogExample : ContentDialog + public partial class ContentDialogExample { + public static readonly DependencyProperty MessageProperty = DependencyProperty.Register( + "Message", typeof(string), typeof(ContentDialogExample), new PropertyMetadata(default(string))); + + public string Message + { + get => (string) GetValue(MessageProperty); + set => SetValue(MessageProperty, value); + } + public ContentDialogExample() { InitializeComponent(); diff --git a/MainWindow.xaml b/MainWindow.xaml index dcf0540..a60c3f2 100644 --- a/MainWindow.xaml +++ b/MainWindow.xaml @@ -66,7 +66,7 @@ - + @@ -119,7 +119,6 @@ - - - + @@ -190,13 +185,12 @@ Command="{Binding ShowDialogCommand}" Label="Hello" Symbol="x³" - Value="World" /> + Value="{Binding WelcomeMessage}" /> - CommandManager.RequerySuggested -= value; } - public bool CanExecute(object parameter) - { - return _canExecute?.Invoke(parameter) ?? true; - } + public bool CanExecute(object parameter) => _canExecute?.Invoke(parameter) ?? true; public void Execute(object parameter) { diff --git a/WindowViewModel.cs b/WindowViewModel.cs index ce2e29e..7213d9f 100644 --- a/WindowViewModel.cs +++ b/WindowViewModel.cs @@ -8,13 +8,15 @@ namespace ModernWpfPlayground { public WindowViewModel() { - ShowDialogCommand = new RelayCommand(async x => await ShowDialogAsync(x)); + ShowDialogCommand = new RelayCommand(async x => await ShowDialogAsync()); + CloseCommand = new RelayCommand(x => Application.Current.Shutdown()); } - private async Task ShowDialogAsync(object obj) + private async Task ShowDialogAsync() { - var dialog = new ContentDialogExample(); - await dialog.ShowAsync(); + var dialog = new ContentDialogExample {Message = WelcomeMessage}; + var result = await dialog.ShowAsync(); + WelcomeMessage = result.ToString(); } public bool BooleanValue @@ -41,6 +43,14 @@ namespace ModernWpfPlayground set => SetProperty(value); } - public ICommand ShowDialogCommand { get; } + public RelayCommand ShowDialogCommand { get; } + + public string WelcomeMessage + { + get => GetProperty("Shadow of the empire"); + set => SetProperty(value); + } + + public ICommand CloseCommand { get; } } } \ No newline at end of file