2011-05-10 163 views
1

我将数据网格分组到两个级别。我的意思是每个主要组都有一个或多个子组。Wpf DataGrid子组风格

<controls:DataGrid.GroupStyle> 
      <GroupStyle> 
       <GroupStyle.HeaderTemplate> 
        <DataTemplate> 
         <StackPanel> 
          <TextBlock Text="{Binding Path=Name}" /> 
         </StackPanel> 
        </DataTemplate> 
       </GroupStyle.HeaderTemplate> 
       <GroupStyle.ContainerStyle> 
        <Style TargetType="{x:Type GroupItem}"> 
         <Setter Property="Template"> 
          <Setter.Value> 
           <ControlTemplate TargetType="{x:Type GroupItem}"> 
            <Expander IsExpanded="True" Style="{DynamicResource newExpanderStyle}" HorizontalAlignment="Left" 
              Margin="5,0,0,0" VerticalAlignment="Top" Background="{DynamicResource NormalBrushGrid}" > 
             <Expander.Header> 
              <StackPanel Background="#E5E5E5" Orientation="Horizontal"> 
               <TextBlock Text="{Binding Path=Name}" FontWeight="Bold" FontSize="12" Margin="5,0" /> 
               <TextBlock Text="{Binding Path=ItemCount}"/> 
              </StackPanel> 
             </Expander.Header> 
             <ItemsPresenter /> 
            </Expander> 
           </ControlTemplate> 
          </Setter.Value> 
         </Setter> 
        </Style> 
       </GroupStyle.ContainerStyle> 
      </GroupStyle> 
     </controls:DataGrid.GroupStyle> 

我想区分的子组从主group.How我可以预先

昌施加不同的颜色来分组头

感谢。

回答

2

这些组不提供太多信息,但如果您只有一个子级别,则可以使用CollectionViewGroup.IsBottomLevel来区分。例如

<GroupStyle.HeaderTemplate> 
    <DataTemplate> 
     <TextBlock Text="{Binding Name}"> 
      <TextBlock.Style> 
       <Style TargetType="{x:Type TextBlock}"> 
        <Style.Triggers> 
         <DataTrigger Value="True"> 
          <DataTrigger.Binding> 
           <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Content.IsBottomLevel"/> 
          </DataTrigger.Binding> 
          <Setter Property="Foreground" Value="Red"/> 
         </DataTrigger> 
        </Style.Triggers> 
       </Style> 
      </TextBlock.Style> 
     </TextBlock> 
    </DataTemplate> 
</GroupStyle.HeaderTemplate> 

模板化亲本是ContentPresenter和该Content是一个内部组类。

+0

Msdn说你不能在XAML中设置这个属性。 – Hukam 2011-05-11 07:57:38

+0

我没有尝试设置它,是吗? – 2011-05-19 23:21:00

+0

请尝试让我知道:) – Hukam 2011-05-20 07:24:07