在WPF中,我们熟悉的ListView控件经常能用dataTemplate创建数据驱动的控件。有的时候其实wrapPanel也可以这样做的。应该说所有的容器控件都可以的吧
在这里呢。我先贴一个wrapPanel动态添加控件的方法:
<WrapPanel Margin="20,0,0,0" >
<ItemsControl ItemsSource="{Binding Path=DeviceMenuItemList}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Width="Auto" IsItemsHost="True"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Margin="56,30,0,0">
<Button Width="40" Height="40" Click="DeviceCommand_Click" Tag="{Binding}"></Button>
<TextBlock Text="{Binding Path=MenuItemName}" Margin="0,20,0,0"></TextBlock>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</WrapPanel>