我试图在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