我的视图模型有一个像这样的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"/>
如何有效地将其显示为单独圈子的集合?
答案 0 :(得分:2)
使用如下所示的绑定转换器。它会转换IEnumerable<Point>
到由一组零长度线组成的StreamGeometry
。这些被绘制为圆圈
通过将StrokeStartLineCap
和StrokeEndLineCap
设置为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"/>