gpt4 book ai didi

wpf - 如何将一组点绘制为单独的圆圈?

转载 作者:行者123 更新时间:2023-12-02 09:21:37 24 4
gpt4 key购买 nike

我的 View 模型有一个 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"/>

我如何有效地将其显示为单独圆圈的集合?

它可以通过 ItemsControl 来完成,例如在其 ItemTemplate 中具有 EllipseGeometry 的 Path 元素,但是这将涉及大量 UI 元素,这对于 PointsCollection 中的大量点可能效果不佳。

最佳答案

如下所示的绑定(bind)转换器可以转换 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"/>

关于wpf - 如何将一组点绘制为单独的圆圈?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42246896/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com