伙计们,所以我有一个wpf应用程序,我想制作一个100x100的正方形网格,并能够在我的代码中将其视为普通集合(列表,数组等);
我怎么能在WPF中做到这一点而又不写<Rectangle .../>
10,000次?
答案 0 :(得分:2)
您可以尝试将WrapPanel
与ItemsControl
组合使用:
<ItemsControl x:Name="RectanglesItemsControl">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="myNamespace:MyType">
<Rectangle Width="{Binding Width}" Height="{Binding Height}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
MyType是具有Width
和Height
属性的简单类,它还需要实现INotifyPropertyChanged
。
然后您可以将ItemsControl
的{{1}}设置为列表,甚至更好的ItemsSource
,以注册集合更改:
ObservableCollection<MyType>
编辑:您可以将RectangleItemsControl.ItemsSource = myLongCollectionFilledWithALotOfRectangles;
替换为任何内容,也可以使用WrapPanel
包含100行和列。