Note that there are some explanatory texts on larger screens.

plurals
  1. POCombining ObservableCollection<T> and List<T> in a MVC application
    primarykey
    data
    text
    <p>I'm trying to display a list of alarms in a WPF ListVieuw. To accomplish this I databinded the Listbox to a property containing the list of alarms. Since I use the MVC programming paradigm the property is located in the controller, and the datacontext of the view is set to that controller.</p> <p>I noticed that when I added an alarm to the list, the view didn't display the new alarm. After some research I found I need to use the ObservableCollection class to do this correctly.</p> <p>However, displaying the list of alarms isn't the only thing that needs to be done with it, so I can't / don't want to change the variable type of the list to ObservableCollection.</p> <p>I now tried to make a property of the type ObservableCollection, but this doesn't work either. This is pretty normal, since I don't add the alarm to the property, I add it to the variable, which is still of the type List.</p> <p>Is there a way to tell the property when the List is updated, or an other/better way to display my alarms and keep them easy to use for other parts of the program?</p> <p>Edit:</p> <p>My workaround: I trigger the <code>PropertyChanged</code> event by clearing my property FutureEvents in the eventhandler of the <code>PropertyChanged</code> event from my alarms variable.</p> <p>My code: class cMain { private static volatile cMain instance; private static object syncRoot = new Object();</p> <pre><code> ObservableCollection&lt;Alarm&gt; alarms; #region properties /// &lt;summary&gt; /// Returns the list of alarms in the model. Can't be used to add alarms, use the AddAlarm method /// &lt;/summary&gt; public ObservableCollection&lt;Alarm&gt; Alarms { get { return alarms; } } /// &lt;summary&gt; /// Returns the ObservableCollection of future alarms in the model to be displayed by the vieuw. /// &lt;/summary&gt; public ObservableCollection&lt;Alarm&gt; FutureAlarms { get { //Only show alarms in the future and alarm that recure in the future var fAlarms = new ObservableCollection&lt;Alarm&gt;(alarms.Where(a =&gt; a.DateTime &gt; DateTime.Now || (a.EndRecurrency != null &amp;&amp; a.EndRecurrency &gt; DateTime.Now))); return fAlarms; } } /// &lt;summary&gt; /// Returns a desctription of the date and time of the next alarm /// &lt;/summary&gt; public String NextAlarmDescription { get { if (alarms != null) { return alarms.Last().DateTimeDescription; } else { return null; } } } #endregion //properties #region public /// &lt;summary&gt; /// Returns the instance of the singleton /// &lt;/summary&gt; 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; } } /// &lt;summary&gt; /// Shows a new intance of the new alarm window /// &lt;/summary&gt; 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&lt;Alarm&gt;(alarms)); } #endregion //public #region private //Constructor is private because cMain is a singleton private cMain() { alarms = new ObservableCollection&lt;Alarm&gt;(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 } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload