using cManagement.Commands;
using cManagement.Models;
using cManagement.Services;
using cManagement.Views;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Data;
namespace cManagement.ViewModels
{
class ContactListingViewModel : BaseViewModel, IDataErrorInfo
{
AppDbContext db = new AppDbContext();
public ContactListingViewModel()
{
//if (ContactRef.Contact == null)
//{
// ContactRef.Contact = new ContactModel();
//}
db.tblContact.Load();
cvs.Source = db.tblContact.Local.ToObservableCollection();
cvs.Filter += ContactFilter;
}
public ICollectionView View { get => cvs.View; }
private CollectionViewSource cvs = new CollectionViewSource();
public ContactModel Contact
{
get => ContactRef.Contact;
set
{
if (ContactRef.Contact != value)
{
ContactRef.Contact = value;
OnPropertyChanged();
}
}
}
#region Commands
public DelegateCommand Cmd { get => new DelegateCommand(CanExec , CmdExec); }
private AddContactView addContactView;
private bool? diagResult;
private void CmdExec(object parameter)
{
switch (parameter.ToString())
{
case "Add":
AddContactViewModel addContactViewModel = new AddContactViewModel() { Contact = new ContactModel() };
addContactView = new AddContactView() { DataContext = addContactViewModel };
diagResult = addContactView.ShowDialog();
if(diagResult.HasValue && diagResult.Value)
{
db.tblContact.Add(Contact);
db.SaveChanges();
}
break;
case "Edit":
if (Contact != null)
{
addContactView = new AddContactView() { DataContext = new AddContactViewModel() { Contact = Contact } };
diagResult = addContactView.ShowDialog();
if (diagResult.HasValue && diagResult.Value)
{
db.SaveChanges();
}
else
{
db.Entry(Contact).State = EntityState.Unchanged;
cvs.View.Refresh();
OnPropertyChanged(nameof(View));
}
}
break;
case "Delete":
if (Contact != null)
{
db.tblContact.Remove(Contact);
db.SaveChanges();
}
break;
default:
break;
}
}
private bool CanExec(object parameter)
{
switch (parameter.ToString())
{
case "Add":
return true;
default:
return (Contact != null);
}
}
#endregion Commands
#region Filter
private string filterContact;
public string FilterContact
{
get
{
return filterContact;
}
set
{
filterContact = value;
OnPropertyChanged();
cvs.View.Refresh();
}
}
private void ContactFilter(object sender, FilterEventArgs e)
{
if (string.IsNullOrWhiteSpace(filterContact))
{
//no filter when no search text is entered
e.Accepted = true;
}
else
{
ContactModel contact = (ContactModel)e.Item;
if (contact.Firstname.StartsWith(filterContact, true, null) ||
contact.Firstname.Contains(filterContact, StringComparison.InvariantCultureIgnoreCase) ||
contact.Lastname.StartsWith(filterContact, true, null) ||
contact.Lastname.Contains(filterContact, StringComparison.InvariantCultureIgnoreCase)
)
{
e.Accepted = true;
}
else
{
e.Accepted = false;
}
}
}
#endregion Filter
#region ErrorInfo
public Dictionary ErrorCollection { get; private set; } = new Dictionary();
public string Error { get => String.Empty; }
public string this[string propertyName]
{
get
{
string result = null;
switch (propertyName)
{
case "FilterContact":
if (string.IsNullOrWhiteSpace(Contact.Firstname))
result = "Please enter firstname or lastname";
else if (Contact.Firstname.Length < 5)
result = "Firstname must be a minimum of 5 characters.";
break;
}
if (ErrorCollection.ContainsKey(propertyName))
{
ErrorCollection[propertyName] = result;
}
else if (result != null)
{
ErrorCollection.Add(propertyName, result);
}
OnPropertyChanged("ErrorCollection");
return result;
}
}
#endregion ErrorInfo
}
}
using cManagement.Commands;
using cManagement.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
namespace cManagement.ViewModels
{
class AddContactViewModel : BaseViewModel, IDataErrorInfo
{
public ContactModel Contact
{
get => ContactRef.Contact;
set
{
if (ContactRef.Contact != value)
{
ContactRef.Contact = value;
OnPropertyChanged();
}
}
}
#region Commands
public DelegateCommand Cmd { get => new DelegateCommand(CanExec, CmdExec); }
private void CmdExec(object parameter)
{
switch (parameter.ToString())
{
case "Save":
WindowRef.DialogResult = true;
break;
case "Cancel":
WindowRef.DialogResult = false;
break;
default:
break;
}
}
private bool CanExec(object parameter)
{
switch (parameter.ToString())
{
case "Cancel":
return true;
default:
return (Contact != null);
}
}
#endregion Commands
#region Propertyname
public string Firstname
{
get { return Contact.Firstname; }
set
{
Contact.Firstname = value;
OnPropertyChanged();
Cmd.OnCanExecuteChanged();
}
}
public string Lastname
{
get { return Contact.Lastname; }
set
{
Contact.Lastname = value;
OnPropertyChanged();
Cmd.OnCanExecuteChanged();
}
}
#endregion Propertyname
#region ErrorInfo
public Dictionary ErrorCollection { get; private set; } = new Dictionary();
public string Error { get => String.Empty; }
public string this[string propertyName]
{
get
{
string result = null;
switch (propertyName)
{
case "Firstname":
if (string.IsNullOrWhiteSpace(Firstname))
result = "Firstname cannot be empty";
else if (Firstname.Length < 5)
result = "Firstname must be a minimum of 5 characters.";
break;
case "Lastname":
if (string.IsNullOrWhiteSpace(Lastname))
result = "Lastname cannot be empty";
else if (Firstname.Length < 5)
result = "Lastname must be a minimum of 5 characters.";
break;
}
if (ErrorCollection.ContainsKey(propertyName))
{
ErrorCollection[propertyName] = result;
}
else if (result != null)
{
ErrorCollection.Add(propertyName, result);
}
OnPropertyChanged("ErrorCollection");
return result;
}
}
#endregion ErrorInfo
#region Attached Property
public Window WindowRef;
public static readonly DependencyProperty AttPropProperty =
DependencyProperty.RegisterAttached("AttProp", typeof(bool), typeof(AddContactViewModel), new PropertyMetadata(false, OnPropChanged));
public static bool GetAttProp(DependencyObject obj) => (bool)obj.GetValue(AttPropProperty);
public static void SetAttProp(DependencyObject obj, bool par) => obj.SetValue(AttPropProperty, par);
private static void OnPropChanged(DependencyObject depObjet, DependencyPropertyChangedEventArgs e)
{
Window WindowRef = depObjet as Window;
if (WindowRef == null) return;
WindowRef.Loaded += (s, e) => ((AddContactViewModel)(WindowRef.DataContext)).WindowRef = WindowRef;
}
#endregion Attached Property
}
}
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace cManagement.ViewModels
{
public class BaseViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
if (!string.IsNullOrEmpty(propertyName))
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
namespace cManagement.Models
{
public class ContactModel
{
public int Id { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
}
}
namespace cManagement.Models
{
public class ContactRef
{
public static ContactModel Contact { get; set; } = new ContactModel();
}
}
using System;
using System.Windows.Input;
namespace cManagement.Commands
{
public class DelegateCommand : ICommand
{
readonly Action