2012-08-06 68 views
5

我正在爲Windows 8開發一個應用程序metro。我使用GridApp(xaml)項目,但我想在每個部分中使用不同的組風格。在gridview中選擇組風格

我的代碼是:

public class GroupTemplateSelector : GroupStyleSelector 
{ 

    public GroupStyle NewsItemGroupStyle { get; set; } 
    public GroupStyle NormalGroupStyle { get; set; } 

    protected override GroupStyle SelectGroupStyleCore(object group, uint level) 
    { 
     // a method that tries to grab an enum off the bound data object 

     if (level == 3) 
     { 
      return NewsItemGroupStyle; 
     } 
     else 
     { 
      return NormalGroupStyle; 
     } 

     throw new ArgumentException("Unexpected group type"); 

    } 
} 

我使用這個類選擇器組風格和XAML

<!-- NewsItemGroupStyle --> 
<GroupStyle x:Key="NewsItemGroupStyle"> 
    <GroupStyle.HeaderTemplate> 
     <DataTemplate> 
     </DataTemplate> 
    </GroupStyle.HeaderTemplate> 
    <GroupStyle.Panel> 
     <ItemsPanelTemplate> 
      <StackPanel Orientation="Vertical" Margin="0,0,80,0" VerticalAlignment="Bottom"/> 
     </ItemsPanelTemplate> 
    </GroupStyle.Panel> 
</GroupStyle> 


<!-- NormalItemGroupStyle --> 
<GroupStyle x:Key="NormalGroupStyle"> 
    <GroupStyle.HeaderTemplate> 
     <DataTemplate> 
      <Grid Margin="1,0,0,6"> 
       <Button 
        AutomationProperties.Name="Group Title" 
        Content="{Binding Title}" 
        Background="Blue" 
        Click="Header_Click" 
        Style="{StaticResource TextButtonStyle}" 
        /> 
      </Grid> 
     </DataTemplate> 
    </GroupStyle.HeaderTemplate> 
    <GroupStyle.Panel> 
     <ItemsPanelTemplate> 
      <VariableSizedWrapGrid Orientation="Vertical" Margin="0,0,80,0"/> 
     </ItemsPanelTemplate> 
    </GroupStyle.Panel> 
</GroupStyle> 

<!-- selector --> 
<common:GroupTemplateSelector 
    x:Key="groupSelector" 
    NewsItemGroupStyle="{StaticResource NewsItemGroupStyle}" 
    NormalGroupStyle="{StaticResource NormalGroupStyle}" /> 

但款式組變化的一次。

+0

你可以看到,如果這[主題](http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/在MSDN上的線程/ 63a5d82c-1ad2-4e24-bfb4-122d5551c5f0 /)可以解答你的問題。 – 2012-08-07 16:35:39

+0

而問題是? – Denis 2012-08-10 07:11:18

+0

我有完全相同的問題,每個人都在這個線程http://social.msdn.microsoft.com/Forums/en-GB/winappswithcsharp/thread/5f12273f-e000-4c96-a4bc-6ccc18a104a0 – krisdyson 2012-09-14 15:58:06

回答

0

正如Lvsti所指出的,GroupStyleSelector只能改變每個級別的風格。例如所有0級組將具有相同的風格,但所有1級組可以有不同的風格。目前不可能在0級具有不同風格的兩個不同組。實際上,對於同一級別的任何組返回的最後一種風格似乎適用於該級別的所有組。這是不幸的,但這是目前的設計。

開發支持,設計支持和方式更真棒善良:http://bit.ly/winappsupport

+1

等待,我在這裏顯然缺少一些東西。你會如何超過0級組?我只見過用於顯示組及其項目的分組gridview(如在GroupedItemsView模板中)。你能告訴我一個分組gridview的例子:a)有多個級別,b)使用groupstyleselector?謝謝! – SelAromDotNet 2013-04-10 22:35:13