Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In answer to your actual question:</p> <p>You should add a DepencyPropertyChanged handler, as you mentioned. In this handler, you should add an event handler to the CollectionChanged property on the new collection and remove the handler from the old collection, like this:</p> <pre><code> public ObservableCollection&lt;WorkItem&gt; WorkItems { get { return (ObservableCollection&lt;WorkItem&gt;)GetValue(WorkItemsProperty); } set { SetValue(WorkItemsProperty, value); } } // Using a DependencyProperty as the backing store for WorkItems. This enables animation, styling, binding, etc... public static readonly DependencyProperty WorkItemsProperty = DependencyProperty.Register("WorkItems", typeof(ObservableCollection&lt;WorkItem&gt;), typeof(DragGrid), new FrameworkPropertyMetadata(null, OnWorkItemsChanged)); private static void OnWorkItemsChanged(object sender, DependencyPropertyChangedEventArgs e) { DragGrid me = sender as DragGrid; var old = e.OldValue as ObservableCollection&lt;WorkItem&gt;; if (old != null) old.CollectionChanged -= me.OnWorkCollectionChanged; var n = e.NewValue as ObservableCollection&lt;WorkItem&gt;; if (n != null) n.CollectionChanged += me.OnWorkCollectionChanged; } private void OnWorkCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { if (e.Action == NotifyCollectionChangedAction.Reset) { // Clear and update entire collection } if (e.NewItems != null) { foreach (WorkItem item in e.NewItems) { // Subscribe for changes on item item.PropertyChanged += OnWorkItemChanged; // Add item to internal collection } } if (e.OldItems != null) { foreach (WorkItem item in e.OldItems) { // Unsubscribe for changes on item item.PropertyChanged -= OnWorkItemChanged; // Remove item from internal collection } } } private void OnWorkItemChanged(object sender, PropertyChangedEventArgs e) { // Modify existing item in internal collection } </code></pre> <p>As gehho explained, it sounds like you are not using the Grid class as originally intended, although you may be too far into development to want to start over at this point. Classes derived from Panel are actually intended only to visually draw/arrange their children, rather than manipulating and enhancing them. Check out <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.aspx" rel="noreferrer">ItemsControl</a> and the <a href="http://msdn.microsoft.com/en-us/library/bb613548.aspx" rel="noreferrer">WPF Content Model</a> to learn more.</p>
 

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