diff options
| author | Jason Lepp <jlepp@arcresources.com> | 2010-11-05 10:22:58 -0600 |
|---|---|---|
| committer | Jason Lepp <jlepp@arcresources.com> | 2010-11-05 10:22:58 -0600 |
| commit | dc521b38304846c4058f099cb3aed6efc05d57bb (patch) | |
| tree | aea7b0e5a93cc1ddc4c1e1901900063a9136fe14 | |
| parent | 5d4fcd1e2dfd8b673b7bc4af43cab770d2b622a3 (diff) | |
| -rw-r--r-- | src/MVPtoMVVM.mvvm/MainWindow.xaml | 3 | ||||
| -rw-r--r-- | src/MVPtoMVVM.mvvm/viewmodels/MainWindowViewModel.cs | 4 | ||||
| -rw-r--r-- | src/MVPtoMVVM.mvvm/viewmodels/Synchronizer.cs | 8 | ||||
| -rw-r--r-- | src/MVPtoMVVM.mvvm/viewmodels/ToDoItemViewModel.cs | 19 |
4 files changed, 20 insertions, 14 deletions
diff --git a/src/MVPtoMVVM.mvvm/MainWindow.xaml b/src/MVPtoMVVM.mvvm/MainWindow.xaml index fc1ec2a..5fabe37 100644 --- a/src/MVPtoMVVM.mvvm/MainWindow.xaml +++ b/src/MVPtoMVVM.mvvm/MainWindow.xaml @@ -4,7 +4,6 @@ Title="MainWindow" Height="350" Width="525">
<DockPanel LastChildFill="False">
<DockPanel.Resources>
- <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
<Style x:Key="ValidationStyle" TargetType="Control">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
@@ -19,7 +18,7 @@ <ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
- <Image Source="alert.png" Visibility="{Binding Path=ShowDueSoonAlert, Converter={StaticResource BooleanToVisibilityConverter}, Mode=OneWay}"/>
+ <Image Source="alert.png" Visibility="{Binding Path=ShowDueSoonAlert, Mode=OneWay}"/>
<TextBox Width="200" Text="{Binding Path=Description, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource ResourceKey=ValidationStyle}" />
<DatePicker SelectedDate="{Binding Path=DueDate, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource ResourceKey=ValidationStyle}" />
<Button Content="Save" Command="{Binding Path=SaveCommand}"></Button>
diff --git a/src/MVPtoMVVM.mvvm/viewmodels/MainWindowViewModel.cs b/src/MVPtoMVVM.mvvm/viewmodels/MainWindowViewModel.cs index e05d305..5ca0d7f 100644 --- a/src/MVPtoMVVM.mvvm/viewmodels/MainWindowViewModel.cs +++ b/src/MVPtoMVVM.mvvm/viewmodels/MainWindowViewModel.cs @@ -24,7 +24,7 @@ namespace MVPtoMVVM.mvvm.viewmodels this.todoItemRepository = todoItemRepository;
AddNewItemCommand = new SimpleCommand(AddNewItem);
CancelChangesCommand = new SimpleCommand(RefreshChanges);
- updater = new Synchronizer<MainWindowViewModel>(PropertyChanged);
+ updater = new Synchronizer<MainWindowViewModel>(() => PropertyChanged);
TodoItems = new ObservableCollection<TodoItemViewModel>();
RefreshChanges();
}
@@ -57,7 +57,7 @@ namespace MVPtoMVVM.mvvm.viewmodels public void Update(Expression<Func<MainWindowViewModel, object>> property)
{
- updater.Update(property);
+ updater.Update(this, property);
}
}
}
\ No newline at end of file diff --git a/src/MVPtoMVVM.mvvm/viewmodels/Synchronizer.cs b/src/MVPtoMVVM.mvvm/viewmodels/Synchronizer.cs index 5c4488b..09c6302 100644 --- a/src/MVPtoMVVM.mvvm/viewmodels/Synchronizer.cs +++ b/src/MVPtoMVVM.mvvm/viewmodels/Synchronizer.cs @@ -6,16 +6,16 @@ namespace MVPtoMVVM.mvvm.viewmodels {
public class Synchronizer<T> where T : INotifyPropertyChanged
{
- private readonly PropertyChangedEventHandler eventHandler;
+ private readonly Func<PropertyChangedEventHandler> eventHandler;
- public Synchronizer(PropertyChangedEventHandler eventHandler)
+ public Synchronizer(Func<PropertyChangedEventHandler> eventHandler)
{
this.eventHandler = eventHandler;
}
- public void Update(Expression<Func<T, object>> property)
+ public void Update(T viewModel, Expression<Func<T, object>> property)
{
- eventHandler(null, new PropertyChangedEventArgs(GetPropertyNameFrom(property)));
+ eventHandler()(viewModel, new PropertyChangedEventArgs(GetPropertyNameFrom(property)));
}
string GetPropertyNameFrom(Expression<Func<T, object>> property)
diff --git a/src/MVPtoMVVM.mvvm/viewmodels/ToDoItemViewModel.cs b/src/MVPtoMVVM.mvvm/viewmodels/ToDoItemViewModel.cs index cfe1ae8..609f5b9 100644 --- a/src/MVPtoMVVM.mvvm/viewmodels/ToDoItemViewModel.cs +++ b/src/MVPtoMVVM.mvvm/viewmodels/ToDoItemViewModel.cs @@ -1,6 +1,8 @@ using System;
using System.Collections.Generic;
using System.ComponentModel;
+using System.Linq.Expressions;
+using System.Windows;
using MVPtoMVVM.domain;
using MVPtoMVVM.repositories;
using System.Linq;
@@ -24,7 +26,7 @@ namespace MVPtoMVVM.mvvm.viewmodels this.todoItemRepository = todoItemRepository;
SaveCommand = new SimpleCommand(Save, CanSave);
DeleteCommand = new SimpleCommand(Delete);
- synchronizer = new Synchronizer<TodoItemViewModel>(PropertyChanged);
+ synchronizer = new Synchronizer<TodoItemViewModel>(() => PropertyChanged);
validations = new Dictionary<string, IValidation>
{
{"Description", new Validation(() => !string.IsNullOrEmpty(Description), "Cannot have an empty description.")},
@@ -60,11 +62,16 @@ namespace MVPtoMVVM.mvvm.viewmodels {
description = value;
IsDirty = true;
- synchronizer.Update(x => x.Description);
+ Update(x => x.Description);
SaveCommand.Changed();
}
}
+ private void Update(Expression<Func<TodoItemViewModel, object>> func)
+ {
+ synchronizer.Update(this, func);
+ }
+
private DateTime dueDate;
public DateTime DueDate
{
@@ -73,17 +80,17 @@ namespace MVPtoMVVM.mvvm.viewmodels {
dueDate = value;
IsDirty = true;
- synchronizer.Update(x => x.DueDate);
- synchronizer.Update(x => x.ShowDueSoonAlert);
+ Update(x => x.DueDate);
+ Update(x => x.ShowDueSoonAlert);
SaveCommand.Changed();
}
}
- public bool ShowDueSoonAlert
+ public Visibility ShowDueSoonAlert
{
get
{
- return DueDate <= DateTime.Today.AddDays(1);
+ return DueDate <= DateTime.Today.AddDays(1) ? Visibility.Visible : Visibility.Hidden;
}
}
|
