2011-05-10 96 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