Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It seems like you might want to use a grouped view for this data instead, but here's a code solution that addresses the question as stated. If you were planning on doing this for more than one property, I'd recommend making Car inherit from DependencyObject or implementing it from INotifyPropertyChanged instead of creating events for each.</p> <pre><code>// The car class itself public class Car { // This event is raised when the production property changes public event EventHandler&lt;PropertyValueChange&lt;DateTime&gt;&gt; ProductionChanged; DateTime _production; // private data public DateTime Production { get { return _production; } set { if (value == _production) return; // don't raise the event if it didn't change var eventArgs = new PropertyValueChange&lt;DateTime&gt;(_production, value); _production = value; // If anyone is "listening," raise the event if (ProductionChanged != null) ProductionChanged(this, eventArgs); } } } // Class that contains the dictionary of production to car lists class Foo { Dictionary&lt;DateTime, ObservableCollection&lt;Car&gt;&gt; ProductionToCars = new Dictionary&lt;DateTime, ObservableCollection&lt;Car&gt;&gt;(); public void Add(Car c) { _Add(c); // We want to be notified when the car's production changes c.ProductionChanged += this.OnCarProductionChanged; } // This is called when a car's value changes, and moves the car void OnCarProductionChanged(object sender, PropertyValueChange&lt;DateTime&gt; e) { Car c = sender as Car; if (c == null) return; ProductionToCars[e.OldValue].Remove(c); _Add(c); } // this actually places the car in the (currently correct) group private void _Add(Car c) { ObservableCollection&lt;Car&gt; collection; // Find the collection for this car's year if (!ProductionToCars.TryGetValue(c.Production, out collection)) { // if we couldn't find it, create it collection = new ObservableCollection&lt;Car&gt;(); ProductionToCars.Add(c.Production, collection); } // Now place him in the correct collection collection.Add(c); } } // This class encapsulates the information we'll pass when the property value changes public class PropertyValueChange&lt;T&gt; : EventArgs { public T OldValue; public T NewValue; public PropertyValueChange(T oldValue, T newValue) { OldValue = oldValue; NewValue = newValue; } } </code></pre>
 

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