繁体   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