2012-08-06 66 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