Partial code for lab assignment 5
1. Implement the class DelegateCommand and place it in the Command folder.
//Class to implement DelegateCommand
namespace Assignment_WPF.Commands
{
///
/// Commanding logic derives from ICommand Interface implements additional functionalities like CommandManager.RequerySuggested for re iterating UI components
///
public class DelegateCommand : ICommand
{
private Action _executeMethod;
private Predicate _canExecute;
public DelegateCommand(Action _executeMethod)
: this(_executeMethod, null)
{
}
public DelegateCommand(Action executeMethod, Predicate canExecute)
{
_executeMethod = executeMethod;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
if (_canExecute == null)
{
return true;
}
return _canExecute(parameter);
}
public void Execute(object parameter)
{
_executeMethod.Invoke(parameter);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
protected virtual void OnCanExecuteChanged()
{
// add this line
CommandManager.InvalidateRequerySuggested();
}
}
}
_______________________
2. Class Employee
public abstract class Employee : IPayable, IComparable,INotifyPropertyChanged
{
#region member variables
private decimal paymentAmount = 0;
public decimal PaymentAmount
{
get { return paymentAmount; }
set { paymentAmount = value; RaisePropertyChanged("PaymentAmount"); }
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName) //don't set to private.
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
__________________
3. Class MainWindow view
________________________
4. Class MainViewModel
///
/// Enum for sorting order.
///
public enum SortingOrder
{
[Description("Ascending")]
Ascending = 1,
[Description("Descending")]
Descending = 2
}
public class MainViewModel:INotifyPropertyChanged
{
#region data members/properties
private ICommand sortByLastNameASC;
........
private ObservableCollection sortedList;
private List> sortingList;
private SortingOrder selectedSorting = SortingOrder.Ascending; // Default is set to 'Ascending'
public SortingOrder SelectedSorting
{
get { return selectedSorting; }
set
{
selectedSorting = value;
RaisePropertyChanged("SelectedSorting");
}
}
public ICommand SortByLastNameASC
{
get { return sortByLastNameASC; }
}
public ObservableCollection SortedList
{
get { return sortedList; }
set { sortedList = value; }
}
public delegate int SortSSNDelegate(object obj1, object obj2, bool isAscending);
IPayable[] payableObjects = new IPayable[32];
public SortSSNDelegate sortDelegate = null;
//The code below is used for binding a combo to an enums in wpf and mvvm
public List> SortingList
{
get
{
if (sortingList == null)
{
sortingList = new List>();
foreach (SortingOrder level in Enum.GetValues(typeof(SortingOrder)))
{
string description;
FieldInfo fieldInfo = level.GetType().GetField(level.ToString());
DescriptionAttribute[] attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attributes != null && attributes.Length > 0) { description = attributes[0].Description; }
else { description = string.Empty; }
KeyValuePair TypeKeyValue =
new KeyValuePair(description, level);
sortingList.Add(TypeKeyValue);
}
}
return sortingList;
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
///
/// Constructor
///
public MainViewModel()
{
SortedList = new ObservableCollection();
payableObjects = new IPayable[16];
InitGrid();//Initialize the payableObjects
sortByLastNameASC = new DelegateCommand((p) => SortByLastNameASCFxn(p));
sortByPayAmountDesc = new DelegateCommand((p) => SortByPayAmountDescFxn(p));
sortDelegate = new SortSSNDelegate(Employee.CompareSSN); //delegate points to method.
resetGrid = new DelegateCommand((p) => ResetGridFxn(p));
ReloadListCollection(payableObjects);
}
void InitGrid()
{
payableObjects[0] = new SalariedEmployee("John", "Smith", "111-11-1111", 700M);
payableObjects[1] = new SalariedEmployee("Antonio", "Smith", "555-55-5555", 800M);
}
private void SortByLastNameASCFxn(object p) //1.
{
Array.Sort(payableObjects);
ReloadListCollection(payableObjects);
}
private void SortByPayAmountDescFxn(object p) //2.
{
if (SelectedSorting == SortingOrder.Descending)
Array.Sort(payableObjects, new PaymentAmountComparer(false));
else if (SelectedSorting == SortingOrder.Ascending)
Array.Sort(payableObjects, new PaymentAmountComparer(true));
ReloadListCollection(payableObjects);
}
private void ResetGridFxn(object p)
{
InitGrid();
ReloadListCollection(payableObjects);
}
private void ReloadListCollection(IPayable[] p)
{
SortedList.Clear();
foreach (Employee e in payableObjects)
{
SortedList.Add(e);
}
}