以下代码为集合中的每个项生成UserControl。数据显示正确,但由于某些我无法理解的原因,所有UserControl都具有相同的大小。
如您所见,Group2包含多个StackPanel。所有这些StackPanel都具有相同的尺寸,并且不根据内容调整大小。
知道为什么吗?是因为ItemsPanelTemplate中定义的StackPanel?
<CollectionViewSource x:Key="test" Source="{Binding}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Column"/>
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
<ItemsControl ItemsSource="{Binding Source={StaticResource test}}" HorizontalAlignment="Center">
<ItemsControl.ItemTemplate>
<DataTemplate>
<controls:Template/>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.GroupStyle>
<GroupStyle>
<GroupStyle.Panel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</GroupStyle.Panel>
</GroupStyle>
</ItemsControl.GroupStyle>
</ItemsControl>