1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
// PurchaseItem implements INotifyPropertyChanged so that the
// application is notified when a property changes. It
// implements IEditableObject so that pending changes can be discarded.
public class PurchaseItem : INotifyPropertyChanged, IEditableObject
{
struct ItemData
{
internal string Description;
internal double Price;
internal DateTime OfferExpires;
}
ItemData copyData;
ItemData currentData;
public PurchaseItem()
: this("New item", 0, DateTime.Now)
{
}
public PurchaseItem(string desc, double price, DateTime endDate)
{
Description = desc;
Price = price;
OfferExpires = endDate;
}
public override string ToString()
{
return String.Format("{0}, {1:c}, {2:D}", Description, Price, OfferExpires);
}
public string Description
{
get { return currentData.Description; }
set
{
if (currentData.Description != value)
{
currentData.Description = value;
NotifyPropertyChanged("Description");
}
}
}
public double Price
{
get { return currentData.Price; }
set
{
if (currentData.Price != value)
{
currentData.Price = value;
NotifyPropertyChanged("Price");
}
}
}
public DateTime OfferExpires
{
get { return currentData.OfferExpires; }
set
{
if (value != currentData.OfferExpires)
{
currentData.OfferExpires = value;
NotifyPropertyChanged("OfferExpires");
}
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
#endregion
#region IEditableObject Members
public void BeginEdit()
{
copyData = currentData;
}
public void CancelEdit()
{
currentData = copyData;
NotifyPropertyChanged("");
}
public void EndEdit()
{
copyData = new ItemData();
}
#endregion
}
public class ItemsForSale : ObservableCollection<PurchaseItem>
{
public ItemsForSale()
{
Add((new PurchaseItem("Snowboard and bindings", 120, new DateTime(2009, 1, 1))));
Add((new PurchaseItem("Inside C#, second edition", 10, new DateTime(2009, 2, 2))));
Add((new PurchaseItem("Laptop - only 1 year old", 499.99, new DateTime(2009, 2, 28))));
Add((new PurchaseItem("Set of 6 chairs", 120, new DateTime(2009, 2, 28))));
Add((new PurchaseItem("My DVD Collection", 15, new DateTime(2009, 1, 1))));
Add((new PurchaseItem("TV Drama Series", 39.985, new DateTime(2009, 1, 1))));
Add((new PurchaseItem("Squash racket", 60, new DateTime(2009, 2, 28))));
}
} |
Partager