Using multiple brushes within a single GeometryDrawing in WPF

6.2k Views Asked by At

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>
4

There are 4 best solutions below

0
On BEST ANSWER

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.

0
On

You can use a visualbrush to achieve that

 <Grid.Background>
           <VisualBrush>
                    <VisualBrush.Visual>
                        <Grid 
                            Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type FrameworkElement}}, Mode=OneWay}" 
                            Height="{Binding Path=ActualHeight, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type FrameworkElement}}, Mode=OneWay}">

                            <Rectangle Fill="Blue" />
                            <Image Source="your image path" Stretch="Uniform" />

                        </Grid>
                    </VisualBrush.Visual>
                </VisualBrush>
            </Grid.Background>
0
On

You can always overwrite the resources of the brushes when you use the GeometryDrawing:

<Image Source="{StaticResource SomeDrawingImage}">
    <Image.Resources>
        <SolidColorBrush x:Key="SomeGradient" Color="Brown" />
        <SolidColorBrush x:Key="SomeOtherGradient" Color="Yellow" />
    </Image.Resources>
</Image>
2
On

you can do that in code behind

var source = image.Source as DrawingImage;
var drawing = source.Drawing as DrawingGroup;
foreach (GeometryDrawing geometryDrawing in drawing.Children)
{
    geometryDrawing.Brush = Brushes.Brown;
}