views:

447

answers:

3

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>
A: 

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;
}
viky
But that doesn't answer the question...
Anvaka
Although I'm sure I could apply code-behind in some manner, I would prefer a XAML-only solution.
emddudley
for a better approach you can use DynamicResource instead of StaticResource in xaml and update your resource from codebehind. you need to use atleast that much code.
viky
A: 

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>
Arcturus
+1  A: 

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.

cplotts