[英]WPF - How to fill the window with squares
大家好,所以我有一个wpf应用程序,我想制作一个100x100的正方形网格,并能够在我的代码中将其像普通集合一样处理(列表,数组等);
如何在WPF中做到这一点而又不写<Rectangle .../>
10,000次?
您可以尝试结合使用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
的ItemsSource
为List,或者甚至更好的设置为ObservableCollection<MyType>
,以注册集合更改:
RectangleItemsControl.ItemsSource = myLongCollectionFilledWithALotOfRectangles;
编辑:您可以将WrapPanel
替换为所需的任何内容,也可以使用<UniformGrid Rows="100" Columns="100" IsItemsHost="True"/>
包含100行和列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.