WPF: What is the generic container control?

Jonathan Allen picture Jonathan Allen · Jan 25, 2010 · Viewed 14.1k times · Source

In HTML the generic container control is a DIV. It doesn't do a anything on its own, but it makes for a great place to hang stuff off.

Likewise in WinForms the generic container control was the Panel. Again, this is what I would use as a place holder to later load other controls.

What should I use for WPF?

Answer

John Bowen picture John Bowen · Jan 25, 2010

I think the closest thing to what you're looking for is a ContentControl. It does no layout of its own and has no default UI (unless you template it to do one or both of those) but can take any object as it's Content property (WPF UIElement or otherwise) and provide any UI for a CLR object through a DataTemplate assigned to its ContentTemplate property. In that respect it provides a good place to inject other content (like a div in HTML). It also happens to be a base class for many of the standard built-in controls: Button, ListBoxItem, UserControl, Window.

WPF panels don't work as well for placeholders because they can't be templated or have children set through bindings, excepting cases where they are contained in other controls that handle injecting bound content, like the ItemsControl-ItemsPresenter-ItemsPanel relationship.