c# xceed PropertyGrid 使用 System.Windows.Media.PointCollection

c# xceed PropertyGrid work with System.Windows.Media.PointCollection

我有问题。 我使用 属性 网格来设置点集合,但程序给我 InvalidOperationException:集合已被更改…

我尝试使用自定义转换器但没有结果:(

我做错了什么?

有人可以写出正确的程序来解决这个问题吗?

非常感谢您的回答。

我写了一个简单的程序,这不是我的项目(我的项目是绝密的:)),但它的工作原理类似。

XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="600">
    <Grid>
        <xctk:PropertyGrid x:Name="workplace_property" ShowSearchBox="False" Background="WhiteSmoke" UpdateTextBoxSourceOnEnterKey="True" ShowAdvancedOptions="True" SnapsToDevicePixels="True" IsMiscCategoryLabelHidden="True" SelectedObject="{Binding}" HorizontalAlignment="Right" Width="251"/>
        <Canvas Name="myCanvas" Margin="0">
        </Canvas>
    </Grid>
</Window>

CS:

using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;
using Xceed.Wpf.Toolkit.PropertyGrid.Attributes;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public Polyline drawobj;
        public MainWindow()
        {
            InitializeComponent();

            drawobj = new Polyline();
            drawobj.Points = new PointCollection() { new Point(10, 20), new Point(40, 20), new Point(150, 150) };
            drawobj.StrokeThickness = 2;
            drawobj.Stroke = new SolidColorBrush(Colors.Black);

            myCanvas.Children.Add(drawobj);

            workplace_property.SelectedObject = new polyline_property(drawobj);   
        }
    }

    public class polyline_property
    {
        private Polyline drawobj;

        public polyline_property(Polyline obj)
        {
            drawobj = obj;
        }

        public PointCollection Points
        {
            get
            {
                return drawobj.Points;
            }
            set
            {
                drawobj.Points = value;
            }
        }

        public Color Color
        {
            get
            {
                return ((SolidColorBrush)drawobj.Stroke).Color;
            }
            set
            {
                ((SolidColorBrush)drawobj.Stroke).Color = value;
            }
        }

        public Double StrokeThickness
        {
            get
            {
                return drawobj.StrokeThickness;
            }
            set
            {
                drawobj.StrokeThickness = value;
            }
        }

        public DoubleCollection StrokeDashArray
        {
            get
            {
                return drawobj.StrokeDashArray;
            }
            set
            {
                drawobj.StrokeDashArray = value;
            }
        }

        public PenLineCap StrokeDashCap
        {
            get
            {
                return drawobj.StrokeDashCap;
            }
            set
            {
                drawobj.StrokeDashCap = value;
            }
        }

        public Double StrokeDashOffset
        {
            get
            {
                return drawobj.StrokeDashOffset;
            }
            set
            {
                drawobj.StrokeDashOffset = value;
            }
        }
    }
}

我用属性修改 class,效果很好! 将 BindingList 与自定义事件一起使用,将 PointCollection 保存到对象。

using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;
using Xceed.Wpf.Toolkit.PropertyGrid.Attributes;

namespace RCFramework
{
    public class polyline_property
    {
        private Polyline drawobj;
        private BindingList<custom_point> point_collection;

        public polyline_property(Polyline obj)
        {
            drawobj = obj;

            point_collection = new BindingList<custom_point>();
            point_collection.AllowEdit = true;
            point_collection.AllowNew = true;
            point_collection.AllowRemove = true;

            foreach (Point pnt in drawobj.Points)
            {
                custom_point newpnt = new custom_point();
                newpnt.X = pnt.X;
                newpnt.Y = pnt.Y;
                point_collection.Add(newpnt);
            }

            point_collection.RaiseListChangedEvents = true;
            point_collection.ListChanged += point_collection_ListChanged;
        }

        private void point_collection_ListChanged(object sender, ListChangedEventArgs e)
        {
            PointCollection pcol = new PointCollection();
            foreach (custom_point pnt in point_collection)
            {
                pcol.Add(new Point(pnt.X, pnt.Y));
            }
            drawobj.Points = pcol;
        }


        public BindingList<custom_point> Points
        {
            get
            {
                point_collection.RaiseListChangedEvents = false;
                point_collection.Clear();
                foreach (Point pnt in drawobj.Points)
                {
                    custom_point newpnt = new custom_point();
                    newpnt.X = pnt.X;
                    newpnt.Y = pnt.Y;
                    point_collection.Add(newpnt);
                }
                point_collection.RaiseListChangedEvents = true;
                return point_collection;
            }
            set
            {

            }
        }

        public Color Color
        {
            get
            {
                return ((SolidColorBrush)drawobj.Stroke).Color;
            }
            set
            {
                ((SolidColorBrush)drawobj.Stroke).Color = value;
            }
        }

        public Double StrokeThickness
        {
            get
            {
                return drawobj.StrokeThickness;
            }
            set
            {
                drawobj.StrokeThickness = value;
            }
        }

        public DoubleCollection StrokeDashArray
        {
            get
            {
                return drawobj.StrokeDashArray;
            }
            set
            {
                drawobj.StrokeDashArray = value;
            }
        }

        public PenLineCap StrokeDashCap
        {
            get
            {
                return drawobj.StrokeDashCap;
            }
            set
            {
                drawobj.StrokeDashCap = value;
            }
        }

        public Double StrokeDashOffset
        {
            get
            {
                return drawobj.StrokeDashOffset;
            }
            set
            {
                drawobj.StrokeDashOffset = value;
            }
        }
    }

    public class custom_point
    {
        public Double X { get; set; }
        public Double Y { get; set; }

        public custom_point()
        {

        }
    }
}