Search code examples
wpfxamlbrushesgeometrydrawing

Using multiple brushes within a single GeometryDrawing in WPF


Is it possible to use multiple brushes within a single GeometryDrawing? I have several geometries that I want to draw with different brushes, and it's rather verbose to have to declare an individual GeometryDrawing for each. I'm looking for a more concise way to express the following:

<DrawingImage x:Key="SomeDrawingImage">
    <DrawingImage.Drawing>
        <DrawingGroup>
            <GeometryDrawing Brush="{StaticResource SomeGradient}">
                <GeometryDrawing.Geometry>
                    <PathGeometry Figures="{StaticResource SomeFigures}">
                        <PathGeometry.Transform>
                            <TransformGroup>
                                <TranslateTransform X="50" />
                            </TransformGroup>
                        </PathGeometry.Transform>
                    </PathGeometry>
                </GeometryDrawing.Geometry>
            </GeometryDrawing>
            <GeometryDrawing Brush="{StaticResource SomeOtherGradient}">
                <GeometryDrawing.Geometry>
                    <PathGeometry Figures="{StaticResource SomeOtherFigures}">
                        <PathGeometry.Transform>
                            <TransformGroup>
                                <TranslateTransform X="100" />
                            </TransformGroup>
                        </PathGeometry.Transform>
                    </PathGeometry>
                </GeometryDrawing.Geometry>
            </GeometryDrawing>
        </DrawingGroup>
    </DrawingImage.Drawing>
</DrawingImage>

Solution

  • In as far as I understand your question, I would say it is not possible to have multiple brushes within a single GeometryDrawing.

    The whole purpose of a GeometryDrawing is to combine a stroke (with the Pen property) and a fill (with the Brush property) ... with a geometry (with the Geometry property).

    To make our xaml more concise, we ourselves have shared not only brushes (which is common) but also geometry ... but your xaml suggests that you are doing the same.