【问题标题】:WPF Multiple CombinedGeometryWPF 多重组合几何
【发布时间】:2018-07-10 23:29:21
【问题描述】:

我希望使用 Union 将一组具有 X、Y、Radius 属性的视图模型绑定到圆的组合几何。然而,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 中包含 CombinedGeometry,如下所示。但是,我不知道如何设置它以使其易于绑定。

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

【问题讨论】:

    标签: wpf binding geometry


    【解决方案1】:

    您在寻找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 个轮廓。我尝试使用 2 组几何组,1 组用于填充,1 组用于轮廓,但是在某些情况下,我需要填充是透明的,这使得该方法无效。
    • @Ying - 您是否尝试过 FillRule 属性的不同选项?此外,如果这不起作用 - 检查 CombinedGeometry 是否可以由其他 CombinedGeometry 对象组成。
    • 是的,CombinedGeometry 可以由其他 CombinedGeometry 对象组成,但我不知道如何设置它,以便我可以简单地将集合绑定到它。查看我编辑的问题
    • @Ying - &lt;EllipseGeometry RadiusX="{Binding Path=ViewModelCollection[0].RadiusX}" ... 其中 ViewModelCollection 是一个索引器 - 你的意思是这样的吗?
    • 我希望能够动态添加和删除将被联合的圈子。你能举个例子说明你的想法是如何做到的吗?
    【解决方案2】:

    此代码产生与 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>
    

    您可以通过Children="{Binding Circles}" in &lt;GeometryGroup&gt; 绑定到集合,而不是所有&lt;EllipseGeometry&gt;

    【讨论】:

      【解决方案3】:

      我有一个类似的问题,并在这里找到了一个 hepfull 帖子: How to make the border trim the child elements?

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

      【讨论】:

      • 其实我的问题已经被group.FillRule = FillRule.Nonzero;解决了
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多