[英]WPF - How to fill the window with squares
Hey guys so I have a wpf application and I want to make a 100x100 grid of squares and be able to treat it like a normal collection (List, Array etc) in my code ; 大家好,所以我有一个wpf应用程序,我想制作一个100x100的正方形网格,并能够在我的代码中将其像普通集合一样处理(列表,数组等);
How could I do that in WPF without writing <Rectangle .../>
10,000 times? 如何在WPF中做到这一点而又不写<Rectangle .../>
10,000次?
You can try to use a WrapPanel
combined with an ItemsControl
to do that : 您可以尝试结合使用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 would be a simple class with a Width
and Height
property, it would also need to implement INotifyPropertyChanged
. MyType是具有Width
和Height
属性的简单类,还需要实现INotifyPropertyChanged
。
You can then set your ItemsControl
's ItemsSource
to your List, or even better, an ObservableCollection<MyType>
, to register the collection changes: 然后,可以将ItemsControl
的ItemsSource
为List,或者甚至更好的设置为ObservableCollection<MyType>
,以注册集合更改:
RectangleItemsControl.ItemsSource = myLongCollectionFilledWithALotOfRectangles;
EDIT: You can replace WrapPanel
by anything you want, you can also use a <UniformGrid Rows="100" Columns="100" IsItemsHost="True"/>
to have 100 rows and columns. 编辑:您可以将WrapPanel
替换为所需的任何内容,也可以使用<UniformGrid Rows="100" Columns="100" IsItemsHost="True"/>
包含100行和列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.