using PersonalFinance; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Data; namespace PersonalFinanceWindow.ViewModel { class ViewModelBase : INotifyPropertyChanged { public static EntityContext ctx; public ViewModelBase() { //ctx = new EntityContext(); ctx = getContextInstance(); PersonalFinance.Entity.Scheduler s = new PersonalFinance.Entity.Scheduler(ctx); } public EntityContext getContextInstance() { if (ctx != null) return ctx; else return new EntityContext(); } protected void Set(ref T oldVal, T newVal, [CallerMemberName]string propertyName="") { oldVal = newVal; NotifyPropertyChanged(propertyName); if(propertyName.Equals("Payments", StringComparison.CurrentCultureIgnoreCase)) { string s = ""; foreach (PersonalFinance.Payment p in (BindingList)(object)newVal) { s += " "+p.PaymentId; } //MessageBox.Show("Property " + propertyName + " set and notify changed. " + "IDs: " + s); } } protected void NotifyPropertyChanged([CallerMemberName] string propertyName="") { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } public event PropertyChangedEventHandler PropertyChanged; public void SaveContext() { ctx.SaveChanges(); } } [ValueConversion(typeof(List), typeof(string))] public class ListToStringConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (targetType != typeof(string)) throw new InvalidOperationException("The target must be a String"); if (value != null) { return String.Join(", ", ((List)value).ToArray()); } else { return null; } } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } }