下面的代码为集合中的每个项目生成一个 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>