How do I implement a custom Brush in WPF?

To answer the question of why you might want to have a custom brush, consider a complex geometry and you want to fill it with a gradient that matches the shape while changing colours radially from the center of the shape (or any arbitrary starting point).

There is no way to do this with a static fill (which eliminates pretty much all the existing brushes). Short of replicating the object over and over and nesting them (and working out the hulling to make it all look right), filling each one with a different colour, the only practical way is to have a brush that can look at the object being filled.

This is actually doable in Forms.

But it doesn't look like there's any way to do it in WPF, which is unfortunate.

In fact, WPF's brushes are at the same time surprisingly powerful AND surprisingly limited. They're clearly designed to some concept model, but I'm not at all sure it's one that's useful beyond some pretty trivial visual effects, and as usual, it's been made more complex with strange (poorly documented) parameters that makes doing the very simple, somewhat complicated.


I had a quick look at the existing brushes using the Reflector. It seems like their implementation is pretty closed up and depends on lots of internal plumbing. While it might be possible to implement your own brush it seems like it is not a supported option. It could even be that the WPF controls are tied tightly to the existing brushes and will not work with a custom one.

Most likely the best way to achieve something resembling custom brushes is to use the DrawingBrush with some complex drawing logic. You can compose drawing brush from complex shapes using other brushes so this should let you achieve the required goal.

Update after edit

As this is for education, you might be best off downloading the Reflector and using that to see how the brushes work. They are not meant to be self-implemented and since they rely on some internal classes to which programmers do not have access normally it will be quite hard to do so.

Though what makes it interesting is that the Brush documentation does have a remark for inheritors to guide in the correct way to inherit from the Brush.

More updates

When poking around I found a quite neat way to achieve something similar on a chinese blog. The trick there is to use a markup extension so it just looks like a brush. In reality it creates a new image brush based on its attributes. Seems he came to the same conclusion that there are some internal classes which prevent the easy implementation.

Tags:

.Net

Wpf

Brush