From 2a1329627f66915bc938f57fdc7ef9a2f2e687ea Mon Sep 17 00:00:00 2001 From: Antony Male Date: Wed, 26 Feb 2014 13:42:29 +0000 Subject: [PATCH] Add 'SetAndNotify' to PropertyChangedBase --- .../Stylet.Samples.HelloDialog/Dialog1View.xaml | 15 ++++++++++++--- .../Dialog1ViewModel.cs | 7 +++++++ Samples/Stylet.Samples.HelloDialog/ShellView.xaml | 10 +++++++--- .../Stylet.Samples.HelloDialog/ShellViewModel.cs | 14 +++++++++++++- Stylet/PropertyChangedBase.cs | 9 +++++++++ 5 files changed, 48 insertions(+), 7 deletions(-) diff --git a/Samples/Stylet.Samples.HelloDialog/Dialog1View.xaml b/Samples/Stylet.Samples.HelloDialog/Dialog1View.xaml index 9f6b139..713f4d2 100644 --- a/Samples/Stylet.Samples.HelloDialog/Dialog1View.xaml +++ b/Samples/Stylet.Samples.HelloDialog/Dialog1View.xaml @@ -1,8 +1,17 @@  - + xmlns:s="http://github.com/canton7/Stylet" + Title="Dialog1View" Height="145" Width="300"> + + + + + - + + + + + diff --git a/Samples/Stylet.Samples.HelloDialog/Dialog1ViewModel.cs b/Samples/Stylet.Samples.HelloDialog/Dialog1ViewModel.cs index fc0783f..85d412b 100644 --- a/Samples/Stylet.Samples.HelloDialog/Dialog1ViewModel.cs +++ b/Samples/Stylet.Samples.HelloDialog/Dialog1ViewModel.cs @@ -8,9 +8,16 @@ namespace Stylet.Samples.HelloDialog { public class Dialog1ViewModel : Screen { + public string Name { get; set; } + public Dialog1ViewModel() { this.DisplayName = "I'm Dialog 1"; } + + public void Close() + { + this.TryClose(true); + } } } diff --git a/Samples/Stylet.Samples.HelloDialog/ShellView.xaml b/Samples/Stylet.Samples.HelloDialog/ShellView.xaml index b7ac1ef..8adad9f 100644 --- a/Samples/Stylet.Samples.HelloDialog/ShellView.xaml +++ b/Samples/Stylet.Samples.HelloDialog/ShellView.xaml @@ -3,7 +3,11 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="http://github.com/canton7/Stylet" Title="ShellView" Height="300" Width="300"> - - - + + + + + + + diff --git a/Samples/Stylet.Samples.HelloDialog/ShellViewModel.cs b/Samples/Stylet.Samples.HelloDialog/ShellViewModel.cs index e06961a..a5ba9e2 100644 --- a/Samples/Stylet.Samples.HelloDialog/ShellViewModel.cs +++ b/Samples/Stylet.Samples.HelloDialog/ShellViewModel.cs @@ -11,18 +11,30 @@ namespace Stylet.Samples.HelloDialog private IWindowManager windowManager; private IDialogFactory dialogFactory; + private string _nameString; + public string NameString + { + get { return this._nameString; } + set { SetAndNotify(ref _nameString, value); } + } + public ShellViewModel(IWindowManager windowManager, IDialogFactory dialogFactory) { this.DisplayName = "Hello Dialog"; this.windowManager = windowManager; this.dialogFactory = dialogFactory; + + this.NameString = "Click the button to show the dialog"; } public void ShowDialog() { var dialogVm = this.dialogFactory.CreateDialog1(); - this.windowManager.ShowDialog(dialogVm); + if (this.windowManager.ShowDialog(dialogVm).GetValueOrDefault()) + this.NameString = String.Format("Your name is {0}", dialogVm.Name); + else + this.NameString = "Dialog cancelled"; } } diff --git a/Stylet/PropertyChangedBase.cs b/Stylet/PropertyChangedBase.cs index 72a5ddd..c9d3424 100644 --- a/Stylet/PropertyChangedBase.cs +++ b/Stylet/PropertyChangedBase.cs @@ -39,5 +39,14 @@ namespace Stylet } }); } + + protected virtual void SetAndNotify(ref T field, T value, [CallerMemberName] string propertyName = "") + { + if (Comparer.Default.Compare(field, value) != 0) + { + field = value; + this.NotifyOfPropertyChange(propertyName); + } + } } }