Sunday, April 13, 2008

ItemsSource tips & tricks

Describe problem.

When we wish to implement the mechanism Drag&Drop of Children for our control in WPF, particularly that our control contains children in its logical tree (in other case attached and inherit properties work incorrect sometimes). Let that control template contains some other controls inherit from ItemControl. We wish add some children to this inherit from ItemControl controls. What can we do?

If we use Items that we have a disagreeable surprise - we should add items to logical tree of internal control (previous to remove from our control's logical tree - it is the problem described above). But we can create collection inherit of IEnumerable and it is bound to ItemsSource of internal controls - all will be worked nice.

Note:

If property ItemsSource isn't equal 'null' that we can work with Items in read-only mode. If ItemsSource equal 'null' that we can full use Items (It is empty after those actions). If we need dynamic change of items we can implement INotifyPropertyChanged interface. The better solution is to use ObservableCollection for this purpose.

No comments: