如果有人對未來感興趣,我找到了一個解決方案。要顯示任何我想要的項目,我使用ListView和Canvas作爲ItemsPanel。爲了顯示兩種對象,我做兩個列表視圖與透明背景:
<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;}
佈局控制像格會做的伎倆好得多。 – HichemSeeSharp
絕對不是。由於我想動態設置對象的位置,因此網格毫無意義。 –