Following on from this post.
I struggled a lot with trying to get the toolbox to work. In the end, the code I wrote wasn't difficult code at all, the problem really was more one of knowing what code I was supposed to be writing in the first place.
Naturally, there's still the sample but I didn't find it very helpful in terms of understanding how the toolbox fits in.
Here's what I ended up with in order to get some kind of toolbox with a list of Activities on it that can be dragged and dropped to my design surface.
- Implement IToolboxService.
- Implement something graphical that displays a list of Activities and works with the IToolboxService implementation to get things dragged onto the design surface.
Some notes on the above.
Firstly, most (if not all) of the samples I've seen seem to implement IToolboxService in its entirety and it turns out that there's quite a lot of code in there and I'm not sure whether I want/need to write it.
In the framework, there's already an abstract class called ToolboxService which looks to do most of the donkey work for IToolboxService and so I went ahead and derived from that rather than implementing IToolboxService. There are about 5 methods to override and I tried to write as little code as possible (not sure if this is enough code but I seem to get away with it);
class MyToolboxService : ToolboxService
{
public MyToolboxService()
{
}
protected override CategoryNameCollection CategoryNames
{
get
{
return (new CategoryNameCollection(
new string[] { "Workflow" }));
}
}
protected override IList GetItemContainers(
string categoryName)
{
// We only have one category so...
return (GetItemContainers());
}
protected override IList GetItemContainers()
{
if (itemContainers == null)
{
itemContainers = new ArrayList();
}
return (itemContainers);
}
protected override void Refresh()
{
}
protected override string SelectedCategory
{
get
{
return (selectedCategory);
}
set
{
selectedCategory = value;
}
}
protected override ToolboxItemContainer
SelectedItemContainer
{
get
{
return (selectedContainer);
}
set
{
selectedContainer = value;
}
}
private ToolboxItemContainer selectedContainer;
private string selectedCategory;
private ArrayList itemContainers;
}
Once I've got my ToolboxService I added it into the services available off my IDesignerHost at initialisation time.
Now, I need something graphical that displays a list of Activities and allows you to add them to the design surface.
I got stuck here for a little while in that I suspected that there was more "Toolbox-like UI" provided for me than there actually seems to be but in the end I woke up tried to build a simple UI.
It's not too difficult to build some kind of ListBox (mine derives from ListView in the end) that functions as a Toolbox but the other thing that I was struggling with was "How does the toolbox UI tell the design surface that someone's dragging across a particular Activity?". In the end, it turned out to be exactly how it should be in that it seems that the Toolbox UI starts a drag-drop operation with a particular data object but, again, the obvious answer didn't come to me for quite a while (even with samples to stare at).
The final small struggle was "Where do I get a list of Activities from to display?". The answer to this seems to be "wherever you like" so I went ahead and had my Toolbox UI configure itself from a little XML file where I can list assemblies and then I try and use reflection to find all Activities within those assemblies and put them onto the Toolbox. It doesn't work perfectly in that I end up with slightly more Activities than I want on my Toolbox but it's not the end of the world and additional code would fix that. The good thing is that if I want to limit my Toolbox to only the custom Activities that I built then it'd be pretty easy to do that.
So, at this point whilst my designer is still not working properly (there's no menu commands, there's no "Save", "Close", "Open" only works once etc. etc.) I now have a Toolbox that has some Activities on it and I can drag and drop those onto the design surface and they get picked up.
Here's how the designer currently looks;
"Onwards and upwards" - lots more things to do with this before it stops being seriously useless :-)
The project file for where I've got up to so far is here.
Posted
Thu, Aug 24 2006 6:31 AM
by
mtaulty