Add 'SetAndNotify' to PropertyChangedBase

This commit is contained in:
Antony Male 2014-02-26 13:42:29 +00:00
parent d859e2e640
commit 2a1329627f
5 changed files with 48 additions and 7 deletions

View File

@ -1,8 +1,17 @@
<Window x:Class="Stylet.Samples.HelloDialog.Dialog1View" <Window x:Class="Stylet.Samples.HelloDialog.Dialog1View"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Dialog1View" Height="300" Width="300"> xmlns:s="http://github.com/canton7/Stylet"
<Grid> Title="Dialog1View" Height="145" Width="300">
<DockPanel LastChildFill="False" Margin="10">
<DockPanel DockPanel.Dock="Top">
<Label DockPanel.Dock="Left">What is your name?</Label>
<TextBox Text="{Binding Name}" HorizontalAlignment="Stretch"/>
</DockPanel>
</Grid> <Grid DockPanel.Dock="Bottom">
<Button HorizontalAlignment="Left" Width="100" IsDefault="True" Command="{s:Action Close}">Save</Button>
<Button HorizontalAlignment="Right" Width="100" IsCancel="True">Cancel</Button>
</Grid>
</DockPanel>
</Window> </Window>

View File

@ -8,9 +8,16 @@ namespace Stylet.Samples.HelloDialog
{ {
public class Dialog1ViewModel : Screen public class Dialog1ViewModel : Screen
{ {
public string Name { get; set; }
public Dialog1ViewModel() public Dialog1ViewModel()
{ {
this.DisplayName = "I'm Dialog 1"; this.DisplayName = "I'm Dialog 1";
} }
public void Close()
{
this.TryClose(true);
}
} }
} }

View File

@ -3,7 +3,11 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="http://github.com/canton7/Stylet" xmlns:s="http://github.com/canton7/Stylet"
Title="ShellView" Height="300" Width="300"> Title="ShellView" Height="300" Width="300">
<Grid> <DockPanel LastChildFill="False" Margin="10">
<Button Command="{s:Action ShowDialog}">Show Dialog</Button> <DockPanel DockPanel.Dock="Top">
</Grid> <Button DockPanel.Dock="Right" Command="{s:Action ShowDialog}">Show Dialog</Button>
<TextBlock DockPanel.Dock="Left" Text="{Binding NameString}"/>
</DockPanel>
</DockPanel>
</Window> </Window>

View File

@ -11,18 +11,30 @@ namespace Stylet.Samples.HelloDialog
private IWindowManager windowManager; private IWindowManager windowManager;
private IDialogFactory dialogFactory; private IDialogFactory dialogFactory;
private string _nameString;
public string NameString
{
get { return this._nameString; }
set { SetAndNotify(ref _nameString, value); }
}
public ShellViewModel(IWindowManager windowManager, IDialogFactory dialogFactory) public ShellViewModel(IWindowManager windowManager, IDialogFactory dialogFactory)
{ {
this.DisplayName = "Hello Dialog"; this.DisplayName = "Hello Dialog";
this.windowManager = windowManager; this.windowManager = windowManager;
this.dialogFactory = dialogFactory; this.dialogFactory = dialogFactory;
this.NameString = "Click the button to show the dialog";
} }
public void ShowDialog() public void ShowDialog()
{ {
var dialogVm = this.dialogFactory.CreateDialog1(); 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";
} }
} }

View File

@ -39,5 +39,14 @@ namespace Stylet
} }
}); });
} }
protected virtual void SetAndNotify<T>(ref T field, T value, [CallerMemberName] string propertyName = "")
{
if (Comparer<T>.Default.Compare(field, value) != 0)
{
field = value;
this.NotifyOfPropertyChange(propertyName);
}
}
} }
} }