簡體   English   中英

WPF-如何用正方形填充窗口

[英]WPF - How to fill the window with squares

大家好,所以我有一個wpf應用程序,我想制作一個100x100的正方形網格,並能夠在我的代碼中將其像普通集合一樣處理(列表,數組等);

如何在WPF中做到這一點而又不寫<Rectangle .../> 10,000次?

您可以嘗試結合使用WrapPanelItemsControl來做到這一點:

<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是具有WidthHeight屬性的簡單類,還需要實現INotifyPropertyChanged

然后,可以將ItemsControlItemsSource為List,或者甚至更好的設置為ObservableCollection<MyType> ,以注冊集合更改:

RectangleItemsControl.ItemsSource = myLongCollectionFilledWithALotOfRectangles;

編輯:您可以將WrapPanel替換為所需的任何內容,也可以使用<UniformGrid Rows="100" Columns="100" IsItemsHost="True"/>包含100行和列。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM