在 WPF 中的单个 GeometryDrawing 中使用多个画笔

发布于 2024-08-15 16:15:40 字数 1709 浏览 3 评论 0原文

是否可以在单个 几何绘图?我想用不同的画笔绘制几个几何图形,并且必须为每个几何图形声明一个单独的 GeometryDrawing ,这是相当冗长的。我正在寻找一种更简洁的方式来表达以下内容:

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

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

没︽人懂的悲伤 2024-08-22 16:15:40

据我了解您的问题,我想说在单个 GeometryDrawing 中不可能有多个画笔

GeometryDrawing 的全部目的是将笔画(使用 Pen 属性)和填充(使用 Brush 属性)...与几何图形(使用 Geometry 属性)结合起来。

为了使我们的 xaml 更加简洁,我们自己不仅共享画笔(这是常见的),而且还共享几何形状......但您的 xaml 表明您也在做同样的事情。

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.

泅人 2024-08-22 16:15:40

你可以在后面的代码中做到这一点

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

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;
}
梦中楼上月下 2024-08-22 16:15:40

使用 GeometryDrawing 时,您始终可以覆盖画笔的资源:

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

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>
我乃一代侩神 2024-08-22 16:15:40

您可以使用视觉画笔来实现这一点

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文