C#wpf如何在多段线点上绘制矩形

C#wpf如何在多段线点上绘制矩形,c#,wpf,polyline,C#,Wpf,Polyline,我试图在画布上画一条多段线,每个点上都有一个矩形。多段线绑定到ViewModel中的点集合 当我尝试为每个点(如下所示)设置DataTemplate时,它在多段线点上不显示矩形 有没有办法在多段线点上显示矩形 稍后,我想通过拖动这些点来调整多段线 <Polyline Points="{Binding EdgePoints, Converter={StaticResource pointCollectionConverter}}" StrokeThickness="2"> &l

我试图在画布上画一条多段线,每个点上都有一个矩形。多段线绑定到ViewModel中的点集合

当我尝试为每个点(如下所示)设置
DataTemplate
时,它在多段线点上不显示矩形

有没有办法在多段线点上显示矩形

稍后,我想通过拖动这些点来调整多段线

<Polyline Points="{Binding EdgePoints, Converter={StaticResource pointCollectionConverter}}" StrokeThickness="2">
    <Polyline.Resources>
        <DataTemplate DataType="{x:Type Point}">
            <Rectangle Width="20" Height="20" Fill="Black"/>
        </DataTemplate>
    </Polyline.Resources>
</Polyline>

下面是我想画矩形的例子


您可以拥有如下所示的视图模型。除了明显的部分之外,它还将PropertyChanged处理程序附加到每个
顶点
或从每个
顶点
附加/分离,以便为
顶点
属性触发PropertyChanged事件。这是更新多段线的点绑定所必需的

public class ViewModelBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

public class Vertex : ViewModelBase
{
    private Point point;

    public Point Point
    {
        get { return point; }
        set { point = value; OnPropertyChanged(); }
    }
}

public class ViewModel : ViewModelBase
{
    public ViewModel()
    {
        Vertices.CollectionChanged += VerticesCollectionChanged;
    }

    public ObservableCollection<Vertex> Vertices { get; }
        = new ObservableCollection<Vertex>();

    private void VerticesCollectionChanged(
        object sender, NotifyCollectionChangedEventArgs e)
    {
        if (e.Action == NotifyCollectionChangedAction.Add)
        {
            foreach (var item in e.NewItems.OfType<INotifyPropertyChanged>())
            {
                item.PropertyChanged += VertexPropertyChanged;
            }
        }
        else if (e.Action == NotifyCollectionChangedAction.Remove)
        {
            foreach (var item in e.OldItems.OfType<INotifyPropertyChanged>())
            {
                item.PropertyChanged -= VertexPropertyChanged;
            }
        }

        OnPropertyChanged(nameof(Vertices));
    }

    private void VertexPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        OnPropertyChanged(nameof(Vertices));
    }
}
最后,Thumb的DragDelta处理程序:

private void ThumbDragDelta(object sender, DragDeltaEventArgs e)
{
    var vertex = (Vertex)((Thumb)sender).DataContext;

    vertex.Point = new Point(
        vertex.Point.X + e.HorizontalChange,
        vertex.Point.Y + e.VerticalChange);
}

您可以使用如下所示的视图模型。除了明显的部分之外,它还将PropertyChanged处理程序附加到每个
顶点
或从每个
顶点
附加/分离,以便为
顶点
属性触发PropertyChanged事件。这是更新多段线的点绑定所必需的

public class ViewModelBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

public class Vertex : ViewModelBase
{
    private Point point;

    public Point Point
    {
        get { return point; }
        set { point = value; OnPropertyChanged(); }
    }
}

public class ViewModel : ViewModelBase
{
    public ViewModel()
    {
        Vertices.CollectionChanged += VerticesCollectionChanged;
    }

    public ObservableCollection<Vertex> Vertices { get; }
        = new ObservableCollection<Vertex>();

    private void VerticesCollectionChanged(
        object sender, NotifyCollectionChangedEventArgs e)
    {
        if (e.Action == NotifyCollectionChangedAction.Add)
        {
            foreach (var item in e.NewItems.OfType<INotifyPropertyChanged>())
            {
                item.PropertyChanged += VertexPropertyChanged;
            }
        }
        else if (e.Action == NotifyCollectionChangedAction.Remove)
        {
            foreach (var item in e.OldItems.OfType<INotifyPropertyChanged>())
            {
                item.PropertyChanged -= VertexPropertyChanged;
            }
        }

        OnPropertyChanged(nameof(Vertices));
    }

    private void VertexPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        OnPropertyChanged(nameof(Vertices));
    }
}
最后,Thumb的DragDelta处理程序:

private void ThumbDragDelta(object sender, DragDeltaEventArgs e)
{
    var vertex = (Vertex)((Thumb)sender).DataContext;

    vertex.Point = new Point(
        vertex.Point.X + e.HorizontalChange,
        vertex.Point.Y + e.VerticalChange);
}

尝试以下操作:如果矩形仅用于拖放,则它们将不属于viewmodel。它只是另一种输入法,就像textbox@ArtemPopov所以我必须在画布上存储所有点,然后?我认为有一种更简单的方法,多段线已经有了点,所以我只能更改它们的模板尝试这样做:如果矩形仅用于拖放,它们将不属于viewmodel。它只是另一种输入法,就像textbox@ArtemPopov所以我必须在画布上存储所有点,然后?我认为有一种更简单的方法,多段线已经有点了,所以我只能更改它们的模板。感谢这个出色的示例。这正是我想要的,谢谢你的好榜样。这正是我想要的