将ObservableCollection <point>绑定到PathFigure

时间:2018-09-14 14:54:44

标签: c# wpf xaml data-binding

我有一个ObservableCollection of Points,我想从中绘制一条折线。到目前为止,我一直在使用像这样的简单转换器:

[ValueConversion(typeof(ObservableCollection<MyPoint>), typeof(PathGeometry))]
public class PointsDisplayConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        ObservableCollection<MyPoint> path = value as ObservableCollection<MyPoint>;
        if (path == null)
            return null;
        if (path.Count == 0)
            return null;

        PathFigure target = new PathFigure();
        target.StartPoint = new Point(path[0].X, path[0].Y);
        target.Segments = new PathSegmentCollection(); 
        for (int i = 1; i < path.Points.Count; i++)
        {
            LineSegment segment = new LineSegment();
            segment.Point = new Point(path[i].X, path[i].Y);
            target.Segments.Add(segment);
        }

        PathGeometry myPathGeometry = new PathGeometry();
        myPathGeometry.Figures = new PathFigureCollection();
        myPathGeometry.Figures.Add(target);

        return myPathGeometry;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

但是,当X,Y属性中的任何一个发生更改(MyPoint本身显然实现了INotifyPropertyChanged)或添加/删除点时,我都需要更新图形,在这种情况下,由于ObservableCollection实例未更改,因此不调用转换器。因此,我需要绑定XAML中的所有属性。到目前为止,我最接近的是:

        <Path Stroke="Green" StrokeThickness="2" Stretch="Fill">
            <Path.Data>
                <PathGeometry>
                    <PathGeometry.Figures>
                        <PathFigure>
                            <PathFigure.StartPoint>
                                <MultiBinding>
                                    <MultiBinding.Converter>
                                        <local:PointConverter/>
                                    </MultiBinding.Converter>
                                    <MultiBinding.Bindings>
                                        <Binding Path="Points[0].X"/>
                                        <Binding Path="Points[0].Y"/>
                                    </MultiBinding.Bindings>
                                </MultiBinding>
                            </PathFigure.StartPoint>
                            <PathFigure.Segments>
                                <PathSegmentCollection>
                                    <LineSegment>
                                        <LineSegment.Point>
                                            <MultiBinding>
                                                <MultiBinding.Converter>
                                                    <local:PointConverter/>
                                                </MultiBinding.Converter>
                                                <MultiBinding.Bindings>
                                                    <Binding Path="Points[1].X"/>
                                                    <Binding Path="Points[1].Y"/>
                                                </MultiBinding.Bindings>
                                            </MultiBinding>
                                        </LineSegment.Point>
                                    </LineSegment>
                                    <!-- ... more copies for Points[2], Points[3] etc follow ... -->
                                </PathSegmentCollection>
                            </PathFigure.Segments>
                        </PathFigure>
                    </PathGeometry.Figures>
                </PathGeometry>
            </Path.Data>
        </Path>

哪个工作方式与我想要的一样,但是显然我不想手动列出列表中的每个项目,并且不知道如何避免这种情况而无需添加其他转换器...

0 个答案:

没有答案