2017-02-15 41 views
2

我的視圖模型具有PointCollection屬性是這樣的:如何將點集合繪製爲單獨的圓圈?

public class ViewModel : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    private PointCollection points; 
    public PointCollection Points 
    { 
     get { return points; } 
     set 
     { 
      points = value; 
      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Points))); 
     } 
    } 
} 

這通常顯示爲一條折線:

<Polyline Points="{Binding Points}" Stroke="Black" StrokeThickness="2"/> 

我將如何有效地顯示出它作爲獨立的圓的集合?

回答

2

使用如下所示的綁定轉換器。它將IEnumerable<Point> 轉換成由一組零長度線組成的StreamGeometry。這些被繪製爲圓圈 ,由具有StrokeStartLineCapStrokeEndLineCap的路徑設置爲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(); 
    } 
} 

的路徑是這樣的:

<Path Data="{Binding Points, Converter={StaticResource LinePointsConverter}}" 
     Stroke="Black" StrokeThickness="5" 
     StrokeStartLineCap="Round" StrokeEndLineCap="Round"/> 
+1

你回答在一分鐘內你自己的問題?或者在我的電腦上出現一些故障? – FCin

+0

@FCin看到這裏:http://stackoverflow.com/help/self-answer – Clemens

+0

好的,謝謝,我不知道:) – FCin