Andrew Stephens
Andrew Stephens

Reputation: 10201

Looking for some WPF advice on user controls/inheritance

I'm fairly new to WPF and wish to create what I call a "widget framework". My app would have a window with a canvas where I can place one or more "widget" user controls, which can be dragged around and resized. All widgets will have a common "chrome", perhaps with buttons along the top to maximise/minimise, open a configuration dialog, etc.

In my winform days I would have created a base class user control containing the common UI elements, with each widget user control inheriting from this, however I believe WPF has no equivalent of user control inheritance.

So I'm looking for pointers to get me started. Does the answer lie in templates, user controls, both? Any code snippets or examples would be greatly appreciated!

To get an idea of what I'm looking for, take a look at the "Chronos WPF" framework on Codeplex - http://chronoswpf.codeplex.com/. It's a widget framework and more - I tried in vain to unpick the widget stuff but the source is a huge beast and is also intertwined with a composite application framework. Alas it's no longer being actively developed either.

Upvotes: 0

Views: 178

Answers (1)

Slugart
Slugart

Reputation: 4680

A possible MVVM based solution for this would look as follows:

  1. Create the chrome as a UserControl and bind it to a ChromeViewModel for the actions
  2. Inside the chrome use a ContentPresenter control which is then bound to a Content property on the ChromeViewModel. This Content property should be a WidgetViewModel which is set at run-time when you create your widgets.
  3. You should specify one DataTemplate to be associated to each WidgetViewModel using the TargetType attribute on the DataTemplate (make sure to put them in their own Resource Dictionary files).

Upvotes: 1

Related Questions