我試圖在WPF ListVieuw中顯示警報列表。爲了做到這一點,我將Listbox數據綁定到了一個包含警報列表的屬性。由於我使用了MVC編程範例,因此該屬性位於控制器中,並且視圖的datacontext被設置爲該控制器。在MVC應用程序中結合ObservableCollection <T>和列表<T>
我注意到,當我向列表中添加一個鬧鐘時,視圖沒有顯示新的鬧鐘。經過一番研究,我發現我需要使用ObservableCollection類來正確地做到這一點。
但是,顯示警報列表不是唯一需要完成的事情,所以我不能/不想將列表的變量類型更改爲ObservableCollection。
我現在試着做一個ObservableCollection類型的屬性,但這也不起作用。這很正常,因爲我沒有將警報添加到屬性中,所以我將它添加到仍然是List類型的變量中。
有沒有辦法在更新列表時告訴屬性,還是其他/更好的方式來顯示我的警報,並保持它們易於用於程序的其他部分?
編輯:
我的解決方法:我觸發PropertyChanged
事件在從我的報警變量PropertyChanged
事件的事件處理程序清除我的財產FutureEvents。
我的代碼: 類CMAIN { 私有靜態揮發CMAIN實例; private static object syncRoot = new Object();
ObservableCollection<Alarm> alarms;
#region properties
/// <summary>
/// Returns the list of alarms in the model. Can't be used to add alarms, use the AddAlarm method
/// </summary>
public ObservableCollection<Alarm> Alarms
{
get
{
return alarms;
}
}
/// <summary>
/// Returns the ObservableCollection of future alarms in the model to be displayed by the vieuw.
/// </summary>
public ObservableCollection<Alarm> FutureAlarms
{
get
{
//Only show alarms in the future and alarm that recure in the future
var fAlarms = new ObservableCollection<Alarm>(alarms.Where(a => a.DateTime > DateTime.Now || (a.EndRecurrency != null && a.EndRecurrency > DateTime.Now)));
return fAlarms;
}
}
/// <summary>
/// Returns a desctription of the date and time of the next alarm
/// </summary>
public String NextAlarmDescription
{
get
{
if (alarms != null)
{
return alarms.Last().DateTimeDescription;
}
else
{
return null;
}
}
}
#endregion //properties
#region public
/// <summary>
/// Returns the instance of the singleton
/// </summary>
public static cMain Instance
{
get
{
if (instance == null) //Check if an instance has been made before
{
lock (syncRoot) //Lock the ability to create instances, so this thread is the only thread that can excecute a constructor
{
if (instance == null) //Check if another thread initialized while we locked the object class
instance = new cMain();
}
}
return instance;
}
}
/// <summary>
/// Shows a new intance of the new alarm window
/// </summary>
public void NewAlarmWindow()
{
vNewAlarm newAlarm = new vNewAlarm();
newAlarm.Show();
}
public void AddAlarm(Alarm alarm)
{
alarms.Add(alarm);
}
public void RemoveAlarm(Alarm alarm)
{
alarms.Remove(alarm);
}
public void StoreAlarms()
{
mXML.StoreAlarms(new List<Alarm>(alarms));
}
#endregion //public
#region private
//Constructor is private because cMain is a singleton
private cMain()
{
alarms = new ObservableCollection<Alarm>(mXML.GetAlarms());
alarms.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(alarms_CollectionChanged);
}
private void alarms_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
FutureAlarms.Clear(); //Needed to trigger the CollectionChanged event of FutureAlarms
StoreAlarms();
}
#endregion //private
}
一個列表沒有實現INotifyPropertyChanged,所以如何提高PropertyChanged事件? – Bitbored 2012-04-21 20:15:11
@Bitbored,你的**控制器**應該實現它。 – 2012-04-21 20:24:39