Modify the visual interface at run time

May include but is not limited to: adding/removing controls at run time; manipulating the visual tree; control life cycle; generating a template dynamically

I’m a little bit confused about this topic, because it seems so simple, yet you can run into very difficult things here. In WPF, things can get complicated quite soon (compared to, for example ASP.NET). One such thing is the difference between logical and visual trees.

Every WPF element is composed of a whole bunch of other elements (which in turn are WPF elements as well). For example, the Button class uses heavily the decorator pattern, and there is a ButtonChrome class involved, and a ContentPresenter – which can load other elements into one, and the rest. So the logical tree for a Button is simple – Button with a text of String. Yet the visual tree is much more complicated. You can dig deeper into the topic by examining these trees with LogicalTreeHelper and VisualTreeHelper classes.

Now how would you add a control at run time? I’d do the following:

Button b = new Button();
B.Content = “Shiny new Button”;

And it actually works. Since Panel.Children is a UIElementCollection you can use methods like Add or RemoveAt (even Clear), so you have the basic List manipulating methods. So much for adding and removing controls dynamically.

A little on control life cycle (the source MSDN article is available here): controls have three main lifetime events:

  • Initialized: this is raised first – called recursively for a panel element. Basically it’s just signals you that the constructor was called and the element is set up – but you shouldn’t take guaranteed that for example data sources are initialized as well.
  • Loaded: when the Loaded event fires, you can take granted a fully built up logical tree. First the container’s Loaded event is fired, then every child’s.
  • Unloaded: the unloaded event is raised when the element is being removed (or its parent is being removed).

I can’t really say much on templates, since they are a distinct objective which I haven’t touched yet – but their time will come.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s