开发者

Subclassing ItemsControl or some other tactic?

I am attempting to create a custom control. This control will have need a binding that works like a listbox or treeview where it can be bound to an ObservableCollection and items added and removed as necessary. The items that are added to the collection, however, won't be directly displayed on the UI. They'll be processed and then added to controls as necessary. For example, if a string representing a full path to a file were passed in, it would be added to a TreeView with each directory being a new node.

I have attempted to create the custom control as a subclass of ItemsControl, and the binding then works, but I can't figure out how to know when an item is added to the control so that I can work on it. I have also considered adding a DependencyProperty of type ObservableCollection to the control and then binding to it according to these instructions: http://msdn.microsoft.com/en-us/library/aa970563.aspx. But setting it up th开发者_JAVA技巧at way doesn't allow me to bind to the DependencyProperty at all.

At this point, I don't care what method I use to get there, just as long as I can get the results from the first paragraph. Can someone please help me out with this?


By overriding OnItemsChanged in your control you can run whatever code you need to whenever an item is added or removed from an ObservableCollection bound to ItemsSource (during population of the Items property). The parameter of this method is the same NotifyCollectionChangedEventArgs you would get if you hooked up to the OC's event directly.

Doing it this way will allow you to take the same action you would do with the new OC property you were thinking about but continue to use the built in ItemsSource property for binding the collection to the control.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜