WPF - Binding an ObservableCollection Dependency Property within a UserControl

Posted by John on Stack Overflow See other posts from Stack Overflow or by John
Published on 2010-05-05T07:53:25Z Indexed on 2010/05/05 7:58 UTC
Read the original article Hit count: 989

I have a control

class DragGrid : Grid { ... }

which inherits from the original grid and enables dragging and resizing its child elements. I need to bind a custom DP named WorkItemsProperty to an observable collection of type WorkItem (implements INotifyPropertyChanged). Each element in the grid is bound to a collection item.

Whenever the user adds a new item dynamically at runtime (the items cannot be declared in XAML!), or deletes an item from that collection, the WorkItems DP on the DragGrid should be updated, and the children in the grid (where each child represents a WorkItem collection item).

My question is how does the DP notify the control about which child element in the grid must be removed, changed ('change' means user dragged an element, or resized it with the mouse) or added, and how would I identify which one of the existing children is the one that needs to be deleted or changed. I understand that this is where the DependencyPropertyChangedCallback comes in. But that only gets called when the DP property is set anew, not when something inside the collection changes (like add, remove item). So in the end, does the DragGrid control somehow need to subscribe to the CollectionChanged event? At what point would I hook up the event handler for that?

© Stack Overflow or respective owner

Related posts about wpf

Related posts about c#