重用 XAML DrawingImage 仅更改 PathGeometry LineSegments 的方法是什么?

发布于 2024-10-17 01:43:39 字数 1529 浏览 3 评论 0原文

我的 WPF 应用程序中有三个按钮,我想要显示三角形图像。我没有使用 Shape 类,因为它对于我的需求来说太重量级了,所以我使用 DrawingImage 来代替。这是我的图像的标记(我会发布一个看起来干净的图像,但由于我是新手,所以还不能这样做):

<Image>
    <Image.Source>
        <DrawingImage>
            <DrawingImage.Drawing>
                <GeometryDrawing Brush="LawnGreen">
                    <GeometryDrawing.Pen>
                        <Pen Brush="Black" Thickness="1" />
                    </GeometryDrawing.Pen>
                    <GeometryDrawing.Geometry>
                        <PathGeometry>
                            <PathFigure   IsClosed="True">
                                <LineSegment Point="-10, 20" />
                                <LineSegment Point="30, 20" />
                            </PathFigure>
                        </PathGeometry>
                    </GeometryDrawing.Geometry>
                </GeometryDrawing>
            </DrawingImage.Drawing>
        </DrawingImage>
    </Image.Source>
</Image>

这是第一个按钮的图像 - 一个不等边三角形,其峰值位于左侧。其他两个按钮的 XAML 几乎相同 - 唯一的变化是线段点分别在其轴的中间和右侧的峰值略有不同。

如何在没有特定线段坐标的情况下为我的 Image.Source->DrawingImage 做出一个声明,然后执行类似的操作

<Button>  
    <Image Source="MyGenericDrawingImage">  
        <!--*Fill in the PathGeometry>PathFigure> LineSegment coordinates for each button-->  
    </Image>  
</Button>

这看起来比必须将几乎相同的代码块全部相邻转储要干净得多。

I have three buttons in my WPF application that I want to display an image of a triangle. I'm not using the Shape class because it's too heavyweight for my needs, so I'm using a DrawingImage instead. Here is the markup for my image (I would post a clean-looking image but can't do it yet as I'm new):

<Image>
    <Image.Source>
        <DrawingImage>
            <DrawingImage.Drawing>
                <GeometryDrawing Brush="LawnGreen">
                    <GeometryDrawing.Pen>
                        <Pen Brush="Black" Thickness="1" />
                    </GeometryDrawing.Pen>
                    <GeometryDrawing.Geometry>
                        <PathGeometry>
                            <PathFigure   IsClosed="True">
                                <LineSegment Point="-10, 20" />
                                <LineSegment Point="30, 20" />
                            </PathFigure>
                        </PathGeometry>
                    </GeometryDrawing.Geometry>
                </GeometryDrawing>
            </DrawingImage.Drawing>
        </DrawingImage>
    </Image.Source>
</Image>

This is the image for the first button - a scalene triangle with its peak on the left. The other two buttons' XAML are almost identical - the only change being the line segment points will be slightly different to peak in the middle and right of their axes, respectively.

How can I make one declaration for my Image.Source->DrawingImage as above only without the specific line segment coordinates, and then do something like

<Button>  
    <Image Source="MyGenericDrawingImage">  
        <!--*Fill in the PathGeometry>PathFigure> LineSegment coordinates for each button-->  
    </Image>  
</Button>

This would look much cleaner than having to dump blocks of nearly identical code all next to each other.

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

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

发布评论

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

评论(2

淡写薰衣草的香 2024-10-24 01:43:39

我知道这已经过时了,但这是我使用的解决方案。我将路径几何图形设置为资源,并将其引用为样式(非常适合重复使用)。要使箭头指向侧面,请重复使用相同的图标并将其旋转 90 度。

在此处输入图像描述

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:sys="clr-namespace:System;assembly=mscorlib">

<Page.Resources>
  <sys:String x:Key="Icon_ArrowDown">
          F1 M 4.439,0.000 L 2.219,3.112 L 0.000,0.000 L 4.439,0.000 Z
      </sys:String>
</Page.Resources>

<StackPanel> 

<Button>
   <Path Data="{StaticResource  Icon_ArrowDown}" Fill="Red" />
</Button>

<Button>
    <Path Data="{StaticResource Icon_ArrowDown}" Fill="Red" />
        <Path.RenderTransform>
            <CompositeTransform Rotation="-90"/>
        </Path.RenderTransform>
    </Path>
</Button>

</StackPanel>
</Page>

I know this is old, but here's the solution I used. I set the path geometry as a resource, and reference it as a style (great for re-use). To make the arrow point sideways, re-use the same icon and rotate it 90 degrees.

enter image description here

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:sys="clr-namespace:System;assembly=mscorlib">

<Page.Resources>
  <sys:String x:Key="Icon_ArrowDown">
          F1 M 4.439,0.000 L 2.219,3.112 L 0.000,0.000 L 4.439,0.000 Z
      </sys:String>
</Page.Resources>

<StackPanel> 

<Button>
   <Path Data="{StaticResource  Icon_ArrowDown}" Fill="Red" />
</Button>

<Button>
    <Path Data="{StaticResource Icon_ArrowDown}" Fill="Red" />
        <Path.RenderTransform>
            <CompositeTransform Rotation="-90"/>
        </Path.RenderTransform>
    </Path>
</Button>

</StackPanel>
</Page>
那片花海 2024-10-24 01:43:39

除非你有完全对​​称、缩放、旋转的几何图形(等等),否则你将不得不对数据进行某种修改以避免“重复”。如果不全等,它确实是不同的几何形状。

也就是说,您也许能够在代码中编辑数据并使用数据绑定来获取编辑后的值。

以下是有关如何将路径数据绑定到视图模型中指定的几何图形的一些信息:

路径绘制与数据绑定

Unless you have perfectly symmetrical, scaled, rotated geometry (etc), then you're going to have to resort to some sort of modification of the data to avoid "repeating" it. It really is different geometry if it isn't congruent.

That said, you might be able to edit the data in code and use data binding to grab the edited values.

Here is some info on how to data bind a path to geometry specified in a view model:

Path drawing and data binding

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