Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>EDIT: Overhauling this answer to be more specific to the question since the tags show you already knew about <a href="http://msdn.microsoft.com/en-us/library/ms743695.aspx" rel="noreferrer"><code>INotifyPropertyChanged</code></a>.</p> <p>You need to implement <code>INotifyPropertyChanged</code> in class <code>A</code> and in class <code>S</code>. Make it so <code>objectA</code> can only be set through a property that will raise the <code>PropertyChanged</code> event on S whenever a property is changed in <code>A</code>. Example:</p> <pre><code>public class A : INotifyPropertyChanged { private string name; public string Name { get { return name; } set { name = value; OnPropertyChanged("Name"); } } private int count; public int Count { get { return count; } set { count = value; OnPropertyChanged("Count"); } } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } </code></pre> <p>... and class <code>S</code>...</p> <pre><code>public class S : INotifyPropertyChanged { private string name, ID; private A objectA; public A ObjectA { get { return objectA; } set { var old = objectA; objectA = value; // Remove the event subscription from the old instance. if (old != null) old.PropertyChanged -= objectA_PropertyChanged; // Add the event subscription to the new instance. if (objectA != null) objectA.PropertyChanged += objectA_PropertyChanged; OnPropertyChanged("ObjectA"); } } void objectA_PropertyChanged(object sender, PropertyChangedEventArgs e) { // Propagate the change to any listeners. Prefix with ObjectA so listeners can tell the difference. OnPropertyChanged("ObjectA." + e.PropertyName); } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } </code></pre> <p>For class <code>R</code>, use <code>ObservableCollection&lt;S&gt;</code> instead of <code>List&lt;S&gt;</code>, and subscribe to its <code>CollectionChanged</code> event, and monitor when objects are added or removed to <code>listOfObjectS</code>. When they are added, subscribe to <code>S</code>'s <code>PropertyChanged</code> events. Then updated <code>R</code>'s view. Example:</p> <pre><code>public class R { protected string name; protected System.Collections.ObjectModel.ObservableCollection&lt;S&gt; ListOfObjectS { get; private set; } public R() { // Use ObservableCollection instead. ListOfObjectS = new ObservableCollection&lt;S&gt;(); // Subscribe to all changes to the collection. ListOfObjectS.CollectionChanged += listOfObjectS_CollectionChanged; } void listOfObjectS_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { if (e.Action == NotifyCollectionChangedAction.Remove) { // When items are removed, unsubscribe from property change notifications. var oldItems = (e.OldItems ?? new INotifyPropertyChanged[0]).OfType&lt;INotifyPropertyChanged&gt;(); foreach (var item in oldItems) item.PropertyChanged -= item_PropertyChanged; } // When item(s) are added, subscribe to property notifications. if (e.Action == NotifyCollectionChangedAction.Add) { var newItems = (e.NewItems ?? new INotifyPropertyChanged[0]).OfType&lt;INotifyPropertyChanged&gt;(); foreach (var item in newItems) item.PropertyChanged += item_PropertyChanged; } // NOTE: I'm not handling NotifyCollectionChangedAction.Reset. // You'll want to look into when this event is raised and handle it // in a special fashion. } void item_PropertyChanged(object sender, PropertyChangedEventArgs e) { if (e.PropertyName.StartsWith("ObjectA.")) { // Refresh any dependent views, forms, controls, whatever... } } } </code></pre>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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