Search code examples
.netwpfdesign-patternstoolbox

Pattern to implement a toolbox in .Net/Wpf (not the visual part)?


I'm building a Wpf app that currently has a toolbox which looks and functions similarly to the Visual Studio toolbox. I have a range of classes that are represented in the toolbox and different types of objects that can each contain some, but not all, of the toolbox items.

As a stopgap measure to get my app up and going, I hardcoded the toolbox functionality into the UserControls. Now I must progress to a better design that isn't hardcoded.

I've got a simple ToolBoxItem class that has properties for Type, Label and Icon. Each class that needs to go into the toolbox is added to a collection of ToolBoxItems and is displaying correctly.

But I am struggling to come up with an elegant solution for two problems:

  • How to create an instance of the class represented by the ToolBoxItem Type in the object being edited.
  • How to hide Toolbox items that are not compatible with the item being edited. I manually do this now with hardcoded properties that each edited class must implement via an interface (ie CanSupportClassX, CanSupportBaseClassY).

My feeling is that delegates and perhaps generics could be a solution but I've only consumed those features in the past and am unsure how to proceed.


Solution

  • A good example of what you're trying to achieve can be found in AvalonDock, which is completely open source. If you want to see how to use AvalonDock with MVVM (i.e. putting tools in windows and so on), check out the SoapBox Core Framework.