绑定动态创建的CombinedGeometry路径?

时间:2013-07-23 18:28:54

标签: c# wpf drawing shapes

我有一个应用程序在屏幕上绘制Slots作为带圆角的矩形。一个插槽可以是一个Section的一部分,它只不过是Slots的集合。

我想直观地标记已分配的插槽。因此,我想绘制一个覆盖所有选定插槽的实体形状。

选择之前:

enter image description here

选择后:

enter image description here

形状必须没有INNER边框,因此组合几何体。

我真的想将CombinedGeometry绑定到所选Slots的Datatemplate。

视图模型

    public ObservableCollection<Slot> Slots
    {
        get { return _slots; }
        set
        {
            if (Equals(value, _slots)) return;
            _slots = value;
            DrawShape();
            OnPropertyChanged();
        }
    }
    private void DrawShape()
    {
        Path = new Path();
        Path.Stroke = Brushes.White;
        Path.StrokeThickness = 3;

        var count = Slots.Count();

        var combined = new CombinedGeometry();
        combined.GeometryCombineMode = GeometryCombineMode.Union;

        for (var i = 0; i < count; i++)
        {
            if (i == 0)
            {
                var rect1 = new Rect(Slots[i].PositionX, Slots[i].PositionY, Slots[i].Width, Slots[i].Height);
                var rect2 = new Rect(Slots[i + 1].PositionX, Slots[i + 1].PositionY, Slots[i + 1].Width,
                                     Slots[i + 1].Height);
                combined.Geometry1 = new RectangleGeometry(rect1) { RadiusX = 5, RadiusY = 5 };
                combined.Geometry2 = new RectangleGeometry(rect2) { RadiusX = 5, RadiusY = 5 };
            }
            else
            {
                var rect = new Rect(Slots[i + 1].PositionX, Slots[i + 1].PositionY, Slots[i + 1].Width, Slots[i + 1].Height);
                combined.Geometry1 = combined.Clone();
                combined.Geometry2 = new RectangleGeometry(rect) { RadiusX = 5, RadiusY = 5 };
            }
        }
        Path.Data = combined;
    }

XAML

        <ItemsControl x:Name="DeclaredSections" Margin="20" ItemsSource="{Binding Path=Layout.Sections}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <Grid/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <!-- DATABIND MY COMBINED SHAPE -->
                    </Grid>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

0 个答案:

没有答案