[英]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.