Wpf 使用ObservableCollection和ItemsControl绘制直线和椭圆

Wpf 使用ObservableCollection和ItemsControl绘制直线和椭圆,wpf,mvvm,Wpf,Mvvm,我试图在画布上的椭圆之间画一组线。我分两步来完成这项工作: 下面XAML的第一部分将ItemsControl绑定到“SingleCL”,使用绑定到“Points”的嵌套ItemsControl可视化行 第二部分,绑定到相同的集合,可视化椭圆 “SingleCL”和“Points”都是可观察的集合 Point类有四个属性:VisualHorPos、VisualVerPos、CenterHorPos和CenterVerPos,并实现INotifyPropertyChanged。属性表示X,Y,而“视

我试图在画布上的椭圆之间画一组线。我分两步来完成这项工作:

  • 下面XAML的第一部分将ItemsControl绑定到“SingleCL”,使用绑定到“Points”的嵌套ItemsControl可视化行
  • 第二部分,绑定到相同的集合,可视化椭圆
  • “SingleCL”和“Points”都是可观察的集合

    Point类有四个属性:VisualHorPos、VisualVerPos、CenterHorPos和CenterVerPos,并实现INotifyPropertyChanged。属性表示X,Y,而“视觉”属性则调整为在放置在画布上时表示视图的左上角,以便在使用鼠标四处移动时,将中心放置在鼠标指针所在的位置

    点在各自的集合中按Y值排序,以便从下到上绘制线

    为了避免(隐藏)点集合中第一个点从0,0到x2,y2的第一条线,我使用PriorityBinding使其长度为0,从而在实践中隐藏它

    结果就是这张图中显示的红色之字形线条

    在下面添加点(图像方面)-首先在集合中-仅显示椭圆,请参见上图中的蓝色点。如果我保存数据并从头开始重新加载,则行将按预期显示

    在集合中的另一个点之后添加点(图像方向)-正确绘制线,但不会更新现有线以考虑新点

    我已经验证了在添加新点和沿Y轴移动点时,点的排序是否正确

    也许我对WPF的要求太高了,但我希望它在添加新点时,而不仅仅是在绑定发生时,根据ObservableCollection“点”中项目的顺序,从一点到另一点划清界限。此外,在移动点时,我希望线会刷新,以便它们总是根据集合中的顺序从下到上绘制

    在我看来,问题在于,一旦创建了线,在集合中移动或添加对象时,线就不会反弹。是否有方法使ItemsControl在基础ObservableCollection中的每个移动/添加/删除操作上重新评估其所有项

    “绘图代码”

    
    
    以及省略号的代码:

    <UserControl x:Class="SMT.View.AutoCalibration.AutoCalibrationMarkerView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" MouseDown="MarkerMouseDown"
             MouseUp="MarkerMouseUp"
             MouseMove="MarkerMouseMove" Width="10" Height="10"
             Background="Transparent">
    <Grid Background="Transparent">
        <Line X1="0" Y1="5" X2="10" Y2="5" Fill="{Binding Color}" Stroke="{Binding Color}" StrokeThickness="0.5"/>
        <Line X1="5" Y1="0" X2="5" Y2="10" Fill="{Binding Color}" Stroke="{Binding Color}" StrokeThickness="0.5"/>
        <Ellipse Width="10" Height="10" Stroke="{Binding Color}" StrokeThickness="0.5" Fill="Transparent"/>
    </Grid>
    
    
    

    解决方法/解决方案


    正如@Juan Pablo Garcia Coello在评论中提出的那样,我最终为省略号和线条创建了不同的集合,后一个集合将被清除并根据需要重新填充。

    我试图提取您的意思,您希望绘制省略号和线条,但在第一次添加之后,它们放错位置了?添加新点时,没有在正确点之间绘制线。从P1-P2-P3开始,从左到右绘制线。在P1和P2之间添加一个点。预期结果是两条新线,P1-Pnew-P2,P1和P2之间的旧线被删除,但Pnew和P2之间的新线被添加,旧线保留。因此,每次您都需要清除可观察点并添加所有排序的点,而不是简单地添加新点。你为什么不能做呢
    <UserControl x:Class="SMT.View.AutoCalibration.AutoCalibrationMarkerView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" MouseDown="MarkerMouseDown"
             MouseUp="MarkerMouseUp"
             MouseMove="MarkerMouseMove" Width="10" Height="10"
             Background="Transparent">
    <Grid Background="Transparent">
        <Line X1="0" Y1="5" X2="10" Y2="5" Fill="{Binding Color}" Stroke="{Binding Color}" StrokeThickness="0.5"/>
        <Line X1="5" Y1="0" X2="5" Y2="10" Fill="{Binding Color}" Stroke="{Binding Color}" StrokeThickness="0.5"/>
        <Ellipse Width="10" Height="10" Stroke="{Binding Color}" StrokeThickness="0.5" Fill="Transparent"/>
    </Grid>