4 using System.Collections.Generic;
5 using System.ComponentModel;
7 using System.Runtime.CompilerServices;
9 namespace SiliconStudio.Presentation.ViewModel
18 private readonly List<string> changingProperties =
new List<string>();
30 protected List<Tuple<string, string>> DependentProperties =
new List<Tuple<string, string>>();
38 if (serviceProvider == null)
throw new ArgumentNullException(
"serviceProvider");
39 ServiceProvider = serviceProvider;
52 protected bool SetValue<T>(ref T field, T value, [CallerMemberName]
string propertyName = null)
54 return SetValue(ref field, value, null,
new[] { propertyName });
67 protected bool SetValue<T>(ref T field, T value, params
string[] propertyNames)
69 return SetValue(ref field, value, null, propertyNames);
83 protected bool SetValue<T>(ref T field, T value, Action updateAction, [CallerMemberName]
string propertyName = null)
85 return SetValue(ref field, value, updateAction,
new[] { propertyName });
99 protected virtual bool SetValue<T>(ref T field, T value, Action updateAction, params
string[] propertyNames)
101 if (propertyNames.Length == 0)
102 throw new ArgumentOutOfRangeException(
"propertyNames",
@"This method must be invoked with at least one property name.");
106 OnPropertyChanging(propertyNames);
108 if (updateAction != null)
110 OnPropertyChanged(propertyNames);
124 protected bool SetValue(Action updateAction, [CallerMemberName]
string propertyName = null)
126 return SetValue(null, updateAction,
new[] { propertyName });
136 protected bool SetValue(Action updateAction, params
string[] propertyNames)
138 return SetValue(null, updateAction, propertyNames);
150 protected bool SetValue(Func<bool> hasChangedFunction, Action updateAction, [CallerMemberName]
string propertyName = null)
152 return SetValue(hasChangedFunction, updateAction,
new[] { propertyName });
164 protected bool SetValue(
bool hasChanged, Action updateAction, [CallerMemberName]
string propertyName = null)
166 return SetValue(() => hasChanged, updateAction,
new[] { propertyName });
178 protected bool SetValue(
bool hasChanged, Action updateAction, params
string[] propertyNames)
180 return SetValue(() => hasChanged, updateAction, propertyNames);
192 protected virtual bool SetValue(Func<bool> hasChangedFunction, Action updateAction, params
string[] propertyNames)
194 if (propertyNames.Length == 0)
195 throw new ArgumentOutOfRangeException(
"propertyNames",
@"This method must be invoked with at least one property name.");
197 bool hasChanged =
true;
198 if (hasChangedFunction != null)
200 hasChanged = hasChangedFunction();
204 OnPropertyChanging(propertyNames);
205 if (updateAction != null)
207 OnPropertyChanged(propertyNames);
218 var propertyChanging = PropertyChanging;
220 foreach (
string propertyName
in propertyNames)
223 if (changingProperties.Contains(propertyName))
224 throw new InvalidOperationException(
string.Format(
"OnPropertyChanging called twice for property '{0}' without invoking OnPropertyChanged between calls.", propertyName));
226 changingProperties.Add(propertyName);
229 if (propertyChanging != null)
231 propertyChanging(
this,
new PropertyChangingEventArgs(propertyName));
233 string name = propertyName;
234 OnPropertyChanging(DependentProperties.Where(x => x.Item1 == name).Select(x => x.Item2).Distinct().ToArray());
244 var propertyChanged = PropertyChanged;
246 foreach (
string propertyName
in propertyNames.Reverse())
248 string name = propertyName;
249 OnPropertyChanged(DependentProperties.Where(x => x.Item1 == name).Select(x => x.Item2).Distinct().Reverse().ToArray());
250 if (propertyChanged != null)
256 if (!changingProperties.Contains(propertyName))
257 throw new InvalidOperationException(
string.Format(
"OnPropertyChanged called for property '{0}' but OnPropertyChanging was not invoked before.", propertyName));
259 changingProperties.Remove(propertyName);
bool SetValue(bool hasChanged, Action updateAction, params string[] propertyNames)
Manages a property modification and its notifications. The first parameter hasChanged should indicate...
virtual void OnPropertyChanged(params string[] propertyNames)
This method will raise the PropertyChanged for each of the property name passed as argument...
ViewModelBase(IViewModelServiceProvider serviceProvider)
A service provider class for view model objects.
virtual 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...
bool SetValue(bool hasChanged, Action updateAction, [CallerMemberName]string propertyName=null)
Manages a property modification and its notifications. The first parameter hasChanged should indicate...
This abstract class represents a basic view model, implementing INotifyPropertyChanging and INotifyPr...
bool SetValue(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...
virtual void OnPropertyChanging(params string[] propertyNames)
This method will raise the PropertyChanging for each of the property name passed as argument...
PropertyChangingEventHandler PropertyChanging
PropertyChangedEventHandler PropertyChanged
bool SetValue(Action updateAction, params string[] propertyNames)
Manages a property modification and its notifications. This method will invoke the provided update ac...
bool SetValue(Action updateAction, [CallerMemberName]string propertyName=null)
Manages a property modification and its notifications. This method will invoke the provided update ac...