How to draw a collection of points as separate circles?

前端 未结 1 1895
无人及你
无人及你 2020-12-21 14:26

My view model has a PointCollection property like this:

public class ViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler Proper         


        
相关标签:
1条回答
  • 2020-12-21 14:58

    A Binding Converter like shown below could convert an IEnumerable<Point> into a StreamGeometry that consists of a set of zero-length lines.

    These could be drawn as circles by a Path with StrokeStartLineCap and StrokeEndLineCap set to Round.

    public class LinePointsConverter : IValueConverter
    {
        public object Convert(
            object value, Type targetType, object parameter, CultureInfo culture)
        {
            var geometry = new StreamGeometry();
            var points = value as IEnumerable<Point>;
    
            if (points != null && points.Any())
            {
                using (var sgc = geometry.Open())
                {
                    foreach (var point in points)
                    {
                        sgc.BeginFigure(point, false, false);
                        sgc.LineTo(point, true, false);
                    }
                }
            }
    
            return geometry;
        }
    
        public object ConvertBack(
            object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
    

    The Path would look like this:

    <Path Data="{Binding Points, Converter={StaticResource LinePointsConverter}}"
          Stroke="Black" StrokeThickness="5"
          StrokeStartLineCap="Round" StrokeEndLineCap="Round"/>
    
    0 讨论(0)
提交回复
热议问题