WPF多个组合几何图形

3

我希望将一组具有X,Y,Radius属性的视图模型与使用Union的圆形CombinedGeometry绑定。但是,似乎CombinedGeometry仅支持2个几何体。

有没有办法绕过这个限制?

我想要实现的示例:

<Path Stroke="Black" StrokeThickness="1" Fill="#CCCCFF">
  <Path.Data>
    <CombinedGeometry GeometryCombineMode="Union" ItemsSource="{Binding Circles}">
      <CombinedGeometry.Template>
        <EllipseGeometry RadiusX="{Binding Radius}" RadiusY="{Binding Radius}" CenterX="{Binding X}" CenterY="{Binding Y}"/>
      </CombinedGeometry.Template>
    </CombinedGeometry>
  </Path.Data>
</Path>

实际上,可以在CombinedGeometry内部使用CombinedGeometries,如下所示。但是,我不知道如何设置才能轻松绑定。

   <Path Stroke="Black" StrokeThickness="1" Fill="#CCCCFF">
        <Path.Data>

        <!-- Combines two geometries using the union combine mode. -->
        <CombinedGeometry GeometryCombineMode="Union">
            <CombinedGeometry.Geometry1>
                <CombinedGeometry GeometryCombineMode="Union">
                    <CombinedGeometry.Geometry1>
                        <EllipseGeometry RadiusX="50" RadiusY="50" Center="200,200" />
                    </CombinedGeometry.Geometry1>
                    <CombinedGeometry.Geometry2>
                        <EllipseGeometry RadiusX="50" RadiusY="50" Center="125,200" />
                    </CombinedGeometry.Geometry2>
                </CombinedGeometry>
            </CombinedGeometry.Geometry1>
            <CombinedGeometry.Geometry2>
                <CombinedGeometry GeometryCombineMode="Union">
                    <CombinedGeometry.Geometry1>
                        <EllipseGeometry RadiusX="50" RadiusY="50" Center="100,100" />
                    </CombinedGeometry.Geometry1>
                    <CombinedGeometry.Geometry2>
                        <EllipseGeometry RadiusX="50" RadiusY="50" Center="150,120" />
                    </CombinedGeometry.Geometry2>
                </CombinedGeometry>
            </CombinedGeometry.Geometry2>
        </CombinedGeometry>
    </Path.Data>
</Path>
3个回答

2

您是否在寻找GeometryGroup

MSDN 代码示例:

<Path Stroke="Black" StrokeThickness="1" Fill="#CCCCFF">     
  <Path.Data>
    <!-- Creates a composite shape from three geometries. -->
    <GeometryGroup FillRule="EvenOdd">
      <LineGeometry StartPoint="10,10" EndPoint="50,30" />
      <EllipseGeometry Center="40,70" RadiusX="30" RadiusY="30" />          
      <RectangleGeometry Rect="30,55 100 30" />
    </GeometryGroup>      
  </Path.Data>  
</Path>

2
它接近我想要的,但缺少联合功能。我需要合并重叠的形状,以便只存在一个轮廓。我尝试使用两组几何图形,一组用于填充,一组用于轮廓,但在某些情况下,我需要填充透明,这使得该方法无效。 - Ying Chan
@Ying - 你尝试过FillRule属性的不同选项吗?如果这样仍然无法解决问题,请检查是否可以将CombinedGeometry组合成其他CombinedGeometry对象。 - Gishu
是的,CombinedGeometry可以由其他CombinedGeometry对象组成,但我不知道如何设置它,以便我可以简单地将集合绑定到它上面。请参见我的编辑后的问题。 - Ying Chan
@Ying - <EllipseGeometry RadiusX="{Binding Path=ViewModelCollection[0].RadiusX}" ... 其中 ViewModelCollection 是一个索引器 - 你是指像这样的东西吗? - Gishu
我想能够动态地添加和删除将被联合的圆。您能否给我一个使用您的想法实现它的示例? - Ying Chan
我不确定绑定是否对您有所帮助。显而易见的方法是使用DataTemplates,将ViewModel对象呈现为EllipseGeometry。然而,整个组合CombinedGeometry对象的业务似乎必须通过自定义代码完成...另请参阅http://stackoverflow.com/questions/1593860 - Gishu

1
这段代码与原帖中的第二个代码产生了相同的结果。
<Path Stroke="Black" StrokeThickness="1" Fill="#CCCCFF">
    <Path.Data>
        <CombinedGeometry GeometryCombineMode="Union">
            <CombinedGeometry.Geometry1>
                <!-- any geometry here -->
                <GeometryGroup/>
            </CombinedGeometry.Geometry1>
            <CombinedGeometry.Geometry2>
                <GeometryGroup FillRule="Nonzero">
                    <EllipseGeometry RadiusX="50" RadiusY="50" Center="200,200" />
                    <EllipseGeometry RadiusX="50" RadiusY="50" Center="125,200" />
                    <EllipseGeometry RadiusX="50" RadiusY="50" Center="100,100" />
                    <EllipseGeometry RadiusX="50" RadiusY="50" Center="150,120" />
                </GeometryGroup>
            </CombinedGeometry.Geometry2>
        </CombinedGeometry>
    </Path.Data>
</Path>

而不是使用所有的<EllipseGeometry>,你可以通过在<GeometryGroup>中绑定到一个集合来进行绑定,例如Children="{Binding Circles}"


0

我有一个类似的问题,并在这里找到了一个有用的帖子: 如何使边框修剪子元素?

您还可以尝试创建一个转换器,该转换器接收该绑定中的集合并按所需方式构建组合几何图形。 我现在就要这样做 :)


实际上我的问题已被 group.FillRule = FillRule.Nonzero; 解决了。 - Chris

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接