当前位置: 动力学知识库 > 问答 > 编程问答 >

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

网友答案:

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.

网友答案:

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;
}
网友答案:

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>
网友答案:

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>
分享给朋友:
您可能感兴趣的文章:
随机阅读: