Introit - call it whatever, but today I’m recycling an old post. And I have one more little confession. When I started writing a new rehosted app today, I found out my old toolbox FAQ was fairly useless. It was even getting to the point where someone had to post this in the forum:
“I found a number of posts online about how to populate a toolbox with the standard activities/icons. Unfortunately they are all wrong in one way or another. Some add internal non user tools, some get the wrong icons, and most are fragile.”
Ouch! (I’m kind of curious what the internal non-user tools bit is talking about, by the way… anyone know?)
Anyway, I actually really appreciated this post from Frank Holden. Criticism is good, but constructive criticism is great. Especially considering that by posting code with real solutions to the problem, he’s unselfishly helping out the whole WF4 community. This is something I think we want to encourage in the forum – reusable code in posts benefits everyone.
As well as today’s rehosting exertions and Frank’s post, there was one other event pushing me in the direction of updating this FAQ. Even developers at MS are resorting to using private Reflection to get Toolbox Icons working in rehosted apps. The saddest part of this is that the private reflection solution to the problem, while completely unsupportable, just looks much quicker and pain-free than the WPF-ness I am about to do today.
While I’m at it, it’s time to start encouraging people to use icons from the DLL which is legal, and where possible encourage use of vector graphics over bitmaps (just for the sake of pretty apps). So anwyay I felt like finally a new post instead of an update to the old is justified, and we can also make it a better FAQ by readdressing the most frequent question first! (Also there is an answer about creating custom toolbox controls which I have been meaning to add for a long time. Tada!)
Question: How do I (legally) make the out-of-box activities have their standard icons in my rehosted designer app? Answer (for ease of explanation, this is broken into multiple parts):
Extract icons in DrawingBrush form from the .Net assembly System.Activities.Presentation. (Please don’t extract them from Microsoft.VisualStudio.Activities.dll. Legal thing.)
[Credit to Brannon King for pointing out the icons can be found in the SAP assembly, and showing how to converting DrawingBrush to Bitmap.]
Teach your toolbox control look for the extracted icon resources. Here is one possible way relying only on public APIs, by restyling the ToolboxControl:
That XAML-ified toolbox style above involves one piece of custom code which is the ToolboxItemConverter class
Fill up your toolbox! Because the above XAML template will look up icons based on the DisplayName property of ToolboxItemWrapper, make sure the display name matches the resource name, e.g. ‘ForEach’ not ‘ForEachWithBodyFactory’. (Or add hacks , like the ‘FlowChart’ hack in the top section.)
Question: How can I use DrawingBrush to create my own icons for custom activities?
Answer [from old FAQ]: The class ActivityDesigner (System.Activities.Presentation) has a property called Icon.
You can set it from XAML:
or your C# code beside (here loading a DrawingBrush resource from an assembly)
Question: How can I show my Custom activity with no Icon or Display Name?
There are multiple options. 1) Subclass WorkflowViewElement instead of ActivityDesigner. (This is also what you would often do creating custom designers for objects which don’t inherit Activity (like PickBranch)). 2) WPF magic – override the default Style or ContentTemplate of ActivityDesigner.
(Note: WorkflowViewElement is quite the blank slate - it even has null content/background by default, which makes it hard to interact with until you provide something.)
Question: Can I create my own toolbox, instead of using ToolboxControl, and still drag-drop stuff into the designer?
Yes! All you need to do is use the correct drag+drop item format. You can do this with some code like this in a mouse event handler:
Question: I’m creating a custom activity designer in VS and I want to add my custom bitmap icon for the Activity in Visual Studio’s toolbox. How do I do that?
The only way I know so far to customize the Visual Studio toolbox icons is using System.Drawing.ToolboxBitmapAttribute on your activity class.
-if you want to use bitmaps from resources, rather than installing actual bitmap files, it seems that you must set the .bmp file build action to ‘Embedded Resource’, which is the only kind System.Drawing.ToolboxBitmapAttribute understands.
-When using the below code, I have had no luck using anything other than 16x16 pixels bitmaps.
-Hot pink seems to be interpreted as transparent by VS.
Question: How can I load those same BMP bitmaps that I used in VS toolbox (which are compiled into my assembly as ‘Embedded Resource’) and reuse them for the ActivityDesigner Icon?
Since the only resources that System.Drawing.ToolboxBitmapAttribute understands are EmbeddedResource, which isn’t easy to get at from WPF XAML, the best way to go is probably writing some code in the code-beside C# file to extract the resource, and update the ActivityDesigner’s Icon property.
Question: How do I convert DrawingBrush to Bitmap type (for e.g. creating .bmp icon files, or using the VS toolbox)?
Here’s code which helps convert DrawingBrush to Bitmap objects in memory. Saving to disk is just a matter of using the appropriate encoder on the BitmapSource and saving to a FileStream.
Question: Can I override the default activity icons globally?
Yes, in at least one way – see this blog post from Kushal where he modifies the default ActivityDesigner appearance/behavior using WPF styles.
[Thanks to Notre for pointing me at that in comments on the original post.]
(If you just want to change the default icon for activities that don’t have a special icon out of the box, a hacky way is to override the default activity designer registration.
var atb = new AttributeTableBuilder(); atb.AddCustomAttributes(typeof(Activity), new DesignerAttribute(typeof(StyleableDefaultActivityDesigner))); MetadataStore.AddAttributeTable(atb.CreateTable());
Question: Does WF Designer toolbox control support subcategories, or functioning as an arbitrary treeview?
No. In comments to the original of this FAQ, ‘Evan’ suggested one alternative, which is to use multiple toolbox controls, where each toolbox control is a whole subcategory on its own. If you need a fully-blown TreeView with deep nested categories then you are probably better off creating a custom toolbox control using the above technique for drag+drop integration.
Question: Why can’t I significantly modify the toolbox appearance via ToolboxItemWrapper? (And how can I add tooltips?)
A. You might notice that ToolboxCategory and ToolboxItemWrapper represent the items in your toolbox, but they are not actually Visuals, i.e. not part of the WPF visual tree. Their role is just to act as the ViewModel (MVVM) for the toolbox, and supply data which WPF can bind the Visuals/Controls to.
The Toolbox class, on the other hand, is part of the visual tree and it exposes customization points that you can use to drastically modify the toolbox item templated appearance. Those customization points are the properties CategoryItemStyle, CategoryTemplate, ToolboxItemStyle, and ToolTemplate. (This is used above.)
Here is a neat example of using this to add tooltips to the rehosted toolbox from Yuanji Wang via the forums.
Got more toolbox/icon questions? Ask in the comments, or forums by all means. (Experience suggests that comments are kind of broken and you might have to post multiple times until you see a success indicator. Comments are moderated to remove spam and don’t appear right away.)