4 using System.Collections;
5 using System.Collections.Generic;
6 using System.Collections.Specialized;
8 using System.Reflection;
9 using System.Runtime.CompilerServices;
11 using SiliconStudio.ActionStack;
12 using SiliconStudio.Presentation.Services;
13 using SiliconStudio.Presentation.ViewModel.ActionStack;
15 namespace SiliconStudio.Presentation.ViewModel
30 private readonly Dictionary<string, object> preEditValues =
new Dictionary<string, object>();
37 : base(serviceProvider)
60 if (collection == null)
throw new ArgumentNullException(
"collection");
61 collection.CollectionChanged += (sender, e) => CollectionChanged(sender, e, name);
75 protected bool SetValueUncancellable<T>(ref T field, T value, [CallerMemberName]
string propertyName = null)
77 return SetValueUncancellable(ref field, value, null,
new[] { propertyName });
92 protected bool SetValueUncancellable<T>(ref T field, T value, Action updateAction, [CallerMemberName]
string propertyName = null)
94 return SetValueUncancellable(ref field, value, updateAction,
new[] { propertyName });
108 protected bool SetValueUncancellable<T>(ref T field, T value, params
string[] propertyNames)
110 return SetValueUncancellable(ref field, value, null, propertyNames);
125 protected bool SetValueUncancellable<T>(ref T field, T value, Action updateAction, params
string[] propertyNames)
127 ActionStack.BeginTransaction();
131 var result = SetValue(ref field, value, updateAction, propertyNames);
136 ActionStack.DiscardTransaction();
150 return SetValueUncancellable(null, updateAction,
new[] { propertyName });
163 return SetValueUncancellable(null, updateAction, propertyNames);
176 protected bool SetValueUncancellable(Func<bool> hasChangedFunction, Action updateAction, [CallerMemberName]
string propertyName = null)
178 return SetValueUncancellable(hasChangedFunction, updateAction,
new[] { propertyName });
191 protected bool SetValueUncancellable(
bool hasChanged, Action updateAction, [CallerMemberName]
string propertyName = null)
193 return SetValueUncancellable(() => hasChanged, updateAction,
new[] { propertyName });
208 return SetValueUncancellable(() => hasChanged, updateAction, propertyNames);
221 protected virtual bool SetValueUncancellable(Func<bool> hasChangedFunction, Action updateAction, params
string[] propertyNames)
223 ActionStack.BeginTransaction();
227 var result = SetValue(hasChangedFunction, updateAction, propertyNames);
232 ActionStack.DiscardTransaction();
237 protected override bool SetValue<T>(ref T field, T value, Action updateAction, params
string[] propertyNames)
239 if (propertyNames.Length == 0)
240 throw new ArgumentOutOfRangeException(
"propertyNames",
@"This method must be invoked with at least one property name.");
244 string concatPropertyName = string.Join(
", ", propertyNames.Select(
s => string.Format(
"'{0}'",
s)));
245 using (ActionStack.BeginEndTransaction(
"Updated " + concatPropertyName))
247 return base.SetValue(ref field, value, updateAction, propertyNames);
254 protected override bool SetValue(Func<bool> hasChangedFunction, Action updateAction, params
string[] propertyNames)
256 if (propertyNames.Length == 0)
257 throw new ArgumentOutOfRangeException(
"propertyNames",
@"This method must be invoked with at least one property name.");
259 if (hasChangedFunction == null || hasChangedFunction())
261 string concatPropertyName = string.Join(
", ", propertyNames.Select(
s => string.Format(
"'{0}'",
s)));
262 using (ActionStack.BeginEndTransaction(
"Updated " + concatPropertyName))
264 return base.SetValue(hasChangedFunction, updateAction, propertyNames);
273 foreach (
string propertyName
in propertyNames.Where(x => x !=
"IsDirty"))
275 PropertyInfo propertyInfo = GetType().GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public);
276 if (propertyInfo != null && propertyInfo.GetSetMethod() != null && propertyInfo.GetSetMethod().IsPublic)
278 preEditValues.Add(propertyName, propertyInfo.GetValue(
this));
282 base.OnPropertyChanging(propertyNames);
288 base.OnPropertyChanged(propertyNames);
290 foreach (
string propertyName
in propertyNames.Where(x => x !=
"IsDirty"))
292 string displayName = string.Format(
"Updated '{0}'", propertyName);
294 if (preEditValues.TryGetValue(propertyName, out preEditValue))
296 var actionItem = CreatePropertyChangeActionItem(displayName, propertyName, preEditValue);
297 ActionStack.Add(actionItem);
299 preEditValues.Remove(propertyName);
327 private void CollectionChanged(
object sender, NotifyCollectionChangedEventArgs e,
string collectionName)
329 string displayName = string.Format(
"Updated collection '{0}' ({1})", collectionName, e.Action);
330 var list = sender as IList;
333 var toIListMethod = sender.GetType().GetMethod(
"ToIList");
334 if (toIListMethod != null)
335 list = (IList)toIListMethod.Invoke(sender,
new object[0]);
337 var actionItem = CreateCollectionChangeActionItem(displayName, list, e);
338 ActionStack.Add(actionItem);
bool SetValueUncancellable(Func< bool > hasChangedFunction, Action updateAction, [CallerMemberName]string propertyName=null)
Manages a property modification and its notifications. A function is provided to check whether the ne...
bool SetValueUncancellable(bool hasChanged, Action updateAction, params string[] propertyNames)
Manages a property modification and its notifications. The first parameter hasChanged should indicate...
bool SetValueUncancellable(Action updateAction, params string[] propertyNames)
Manages a property modification and its notifications. This method will invoke the provided update ac...
bool SetValueUncancellable(bool hasChanged, Action updateAction, [CallerMemberName]string propertyName=null)
Manages a property modification and its notifications. The first parameter hasChanged should indicate...
A service provider class for view model objects.
override void OnPropertyChanging(params string[] propertyNames)
This method will raise the PropertyChanging for each of the property name passed as argument...
override bool SetValue(Func< bool > hasChangedFunction, Action updateAction, params string[] propertyNames)
Manages a property modification and its notifications. A function is provided to check whether the ne...
virtual bool SetValueUncancellable(Func< bool > hasChangedFunction, Action updateAction, params string[] propertyNames)
Manages a property modification and its notifications. A function is provided to check whether the ne...
An abstact class that inherits from ActionItem and can be used to manage actions related to an IDirti...
void RegisterMemberCollectionForActionStack(string name, INotifyCollectionChanged collection)
Registers the given collection to create CollectionChangedViewModelActionItem in the action stack whe...
virtual ViewModelActionItem CreateCollectionChangeActionItem(string displayName, IList list, NotifyCollectionChangedEventArgs args)
Creates an instance of ViewModelActionItem corresponding to the action of modifying an observable col...
This abstract class is an implementation of ViewModelBase that uses a dispatcher to invoke OnProperty...
This class is an implementation of the DispatcherViewModel class that supports undo/redo of property ...
virtual ViewModelActionItem CreatePropertyChangeActionItem(string displayName, string propertyName, object preEditValue)
Creates an instance of ViewModelActionItem corresponding to the action of modifying a property of thi...
EditableViewModel(IViewModelServiceProvider serviceProvider)
Initializes a new instance of the EditableViewModel class.
bool SetValueUncancellable(Action updateAction, [CallerMemberName]string propertyName=null)
Manages a property modification and its notifications. This method will invoke the provided update ac...
This class represents a thread-safe stack of action items that can be undone/redone.
Base interface for a transactional action stack.
override void OnPropertyChanged(params string[] propertyNames)
This method will raise the PropertyChanged for each of the property name passed as argument...