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,而“视
以及省略号的代码:
<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>