Extending ItemsControl in WPF/Silverlight remark

Have you ever tried to extend ItemsControl in WPF/Silverlight? I guess the answer is Yes. In many situations you need to create a custom control, which support items. In this case you can use either ItemsControl or ListBox as a base. ItemsControl provides a couple of methods to help you build your items control. Such methods are IsItemItsOwnContainerOverride, GetContainerForItemOverride and PrepareContainerForItemOverride. When you add items to your control these methods are being called to help you manage your items. But have you run is such situation that when you added items these methods were not called? Well, it’s a very strange situation really. Here is you should make sure when you encounter this problem:

  • make sure you are extending ItemsControl
  • make sure your ItemsSource actually has items
  • make sure the control is visible
  • make sure you have put ItemsPresenter in the Template of your control

ItemsPresenter specifies where items should be placed, so always make sure you have placed it along other controls in the Template of your items control.