如果有人对未来感兴趣,我已经找到了解决方案。为了在任何我想要的地方显示项目,我使用 ListView 和 Canvas 作为 ItemsPanel。为了显示两种对象,我制作了两个具有透明背景的 ListView:
<ListView ItemsSource="{Binding collection1}">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<Canvas />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<Rectangle Margin="{Binding mar}" Width="30" Height="30" Stroke="Black" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<ListView ItemsSource="{Binding collection2}">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<Canvas />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<Line X1="{Binding x1}" X2="{Binding x2}" Y1="{Binding y1}" Y2="{Binding y2}" Stroke="Black" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
在代码隐藏中:
public class object1
{
private double x0;
private double y0;
public Thickness mar { get { return new Thickness(x0,y0,0,0); } }
}
public class object2
{
public x1 {get;set;}
public x2 {get;set;}
public y1 {get;set;}
public y2 {get;set;}
}
public ObservableCollection<object1> collection1 {get;set;}
public ObservableCollection<object2> colletcion2 {get;set;}