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

时间:2018-12-27 13:19:01

标签: c# wpf

伙计们,所以我有一个wpf应用程序,我想制作一个100x100的正方形网格,并能够在我的代码中将其视为普通集合(列表,数组等);

我怎么能在WPF中做到这一点而又不写<Rectangle .../> 10,000次?

1 个答案:

答案 0 :(得分:2)

您可以尝试将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

然后您可以将ItemsControl的{​​{1}}设置为列表,甚至更好的ItemsSource,以注册集合更改:

ObservableCollection<MyType>

编辑:您可以将RectangleItemsControl.ItemsSource = myLongCollectionFilledWithALotOfRectangles; 替换为任何内容,也可以使用WrapPanel包含100行和列。

相关问题