Search code examples
.netwpfbrush

How do I implement a custom Brush in WPF?


Where can I find out enough info about how Brushes work to implement my own System.Windows.Media.Brush? I can handle all of the freezable baggage, but it's not really obvious what I need to override to get it to work.


Yeah, so I didn't mean that I want to use a predefined brush. I want to extend System.Windows.Media.Brush, which is an abstract class. This is all purely for my own edification. I'm not even sure what kind of brush I could make. I was just trying to learn how brushes work. As in:

public AwesomeBrush : Brush
{

    protected override Freezable CreateInstanceCore()
    {
        return new AwesomeBrush();
    }

    ... // concrete brush stuff

}

Solution

  • You cannot make a custom WPF brush by inheriting from the System.Windows.Media.Brush class, because that class contains abstract members that are internal.

    If you use Reflector to view the source code of System.Windows.Media.Brush class, you will see the following internal abstract methods:

    internal abstract DUCE.ResourceHandle AddRefOnChannelCore(DUCE.Channel channel);
    internal abstract DUCE.Channel GetChannelCore(int index);
    internal abstract int GetChannelCountCore();
    internal abstract DUCE.ResourceHandle GetHandleCore(DUCE.Channel channel);
    internal abstract void ReleaseOnChannelCore(DUCE.Channel channel);
    

    These must be overridden but cannot be because they are internal.

    Note: Edited my answer because previous answer was about System.Drawing.Brush and irrelevant to this question. Special thanks to Mikko Rantanen for his comment.