萬一來自未來的人遇到這種情況。我能夠嵌套組並對它們進行樣式設置,以便通過執行以下操作使它們不會堆疊在一起。
添加屬性到CollectionViewSource:在DataGrid XAML
<Window.Resources>
<CollectionViewSource x:Key="cvs" Source="{Binding TestData}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="TopProperty"/>
<PropertyGroupDescription PropertyName="SubProperty"/>
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
</Window.Resources>
然後,你必須指定2個GroupStyles,而第二個將用於嵌套組。我向第二組的StackPanel添加了邊距,以便將文本向右推,使其看起來像在適當的列中。
<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<StackPanel>
<Border Background="#FF959595" BorderBrush="#FF727272" BorderThickness="0,0,0,1" Margin="5,0,0,0">
<StackPanel Height="23" Orientation="Horizontal" Margin="3,0,0,0" Background="#FFE6E6E6">
<TextBlock FontWeight="Bold" Text="{Binding Path=Name}" Margin="5,0,0,0" Width="100" VerticalAlignment="Center"/>
</StackPanel>
</Border>
<ItemsPresenter />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<StackPanel>
<Border Background="#FF959595" BorderBrush="#FF727272" BorderThickness="0,0,0,1" Margin="5,0,0,0">
<StackPanel Height="23" Orientation="Horizontal" Margin="3,0,0,0" Background="#FFF3F3F3">
<TextBlock FontWeight="Bold" Text="{Binding Path=Name}" Margin="55,0,0,0" Width="100" VerticalAlignment="Center"/>
</StackPanel>
</Border>
</StackPanel>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</DataGrid.GroupStyle>
我能找到更多關於這個在這裏: https://msdn.microsoft.com/en-us/library/ff407126%28v=vs.110%29.aspx
您正在使用的ICollectionView做分組? – Stephan 2010-05-07 20:12:51
是的,我正在使用ICollectionView來執行分組。 – Keith 2010-05-07 20:57:24