路径图和数据绑定 [英] Path drawing and data binding

查看:105
本文介绍了路径图和数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种能够使用wpf Path元素来绘制路由的方法,该路径将在地图上表示路由。我有一个Route类,其中包含一组顶点,并希望使用它来进行绑定。我真的不知道如何开始..
任何提示?

I am looking for a way to be able to use the wpf Path element to draw a path that will represent a route on the map. I have the Route class that contains a collection of vertices and would like to use it for binding. I don't really know how to even start.. Any hints?

推荐答案

主要的事情你会需要绑定是一个转换器,将您的点转换为几何,路径将需要为数据,这里是什么我的单向转换器从 System.Windows.Point -array到几何看起来像:_

The main thing you'll need for the binding is a converter that turns your points into Geometry which the path will need as Data, here is what my one-way converter from a System.Windows.Point-array to Geometry looks like:

[ValueConversion(typeof(Point[]), typeof(Geometry))]
public class PointsToPathConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        Point[] points = (Point[])value;
        if (points.Length > 0)
        {
            Point start = points[0];
            List<LineSegment> segments = new List<LineSegment>();
            for (int i = 1; i < points.Length; i++)
            {
                segments.Add(new LineSegment(points[i], true));
            }
            PathFigure figure = new PathFigure(start, segments, false); //true if closed
            PathGeometry geometry = new PathGeometry();
            geometry.Figures.Add(figure);
            return geometry;
        }
        else
        {
            return null;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }

    #endregion
}

现在所有这一切都是为了创建一个实例,并将其用作绑定的转换器。在XAML中可能是什么样子:

Now all that is really left is to create an instance of it and use it as the converter for the binding. What it might look like in XAML:

<Grid>
    <Grid.Resources>
        <local:PointsToPathConverter x:Key="PointsToPathConverter"/>
    </Grid.Resources>
    <Path Data="{Binding ElementName=Window, Path=Points, Converter={StaticResource ResourceKey=PointsToPathConverter}}"
          Stroke="Black"/>
</Grid>

如果您需要绑定来自动更新,您应该使用依赖属性或接口,如 INotifyPropertyChanged / INotifyCollectionChanged



希望有助于:D

If you need the binding to update automatically you should work with dependency properties or interfaces like INotifyPropertyChanged/INotifyCollectionChanged

Hope that helps :D

这篇关于路径图和数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆