WPF多重CombinedGeometry

时间:2011-05-03 04:33:05

标签: wpf binding geometry

我希望使用Union将具有X,Y,Radius属性的视图模型集合绑定到Circle的CircleGeometry。但是,似乎CombinedGeometry只支持2个Geometries。

无论如何都有这种限制吗?

我的目标

的例子
<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 个答案:

答案 0 :(得分:0)

您在寻找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>

答案 1 :(得分:0)

我有类似的问题,在这里发现了一个很好的帖子: How to make the border trim the child elements?

您还可以尝试创建一个转换器,该转换器接收该绑定中的集合,并根据需要构建组合几何。 我现在就完全这样做了:)

答案 2 :(得分:0)

此代码产生的结果与OP的第二个代码相同:

<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>中的Children="{Binding Circles}"绑定到集合,而不是全部<GeometryGroup>