如何在Metro GridView中创建组使用不同的布局?

chr*_*son 12 c# microsoft-metro windows-runtime

我正在写一个Windows 8 Metro应用程序.我正在尝试使用三个组绘制GridView.我希望其中一个组以不同于其他组的方式布置项目.我之前在WPF中使用过选择器,所以我认为这是一条好路线.所以我尝试了GroupStyleSelector,我在MSDN上找到了这个例子:

public class ListGroupStyleSelector : GroupStyleSelector
{
  protected override GroupStyle SelectGroupStyleCore(object group, uint level)
  {
    return (GroupStyle)App.Current.Resources["listViewGroupStyle"];
  }
}
Run Code Online (Sandbox Code Playgroud)

所以我改编/扩展了适合我的东西:

CS:

public class ExampleListGroupStyleSelector : GroupStyleSelector
{
  public ExampleListGroupStyleSelector ()
  {
     OneBigItemGroupStyle = null;
     NormalGroupStyle = null;
  }

  public GroupStyle OneBigItemGroupStyle { 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
     var exampleListType= GetExampleListType( group );

     if ( exampleListType== ExampleListType.A)
     {
        return OneBigItemGroupStyle;
     }
     if ( exampleListType== ExampleListType.B|| exampleListType== ExampleListType.B)
     {
        return NormalGroupStyle;
     }

     throw new ArgumentException( "Unexpected group type" );
  }
}
Run Code Online (Sandbox Code Playgroud)

XAML:

<Page.Resources>
  <ExampleListGroupStyleSelector 
     x:Key="ExampleListGroupStyleSelector"
     OneBigItemGroupStyle="{StaticResource OneBigGroupStyle}"
     NormalGroupStyle="{StaticResource NormalGroupStyle}" />
</Page.Resources>
<GridView
     ItemsSource="{Binding Source={StaticResource exampleListsViewSource}}"
     GroupStyleSelector="{StaticResource ExampleListGroupStyleSelector}">
     <GridView.ItemsPanel>
        <ItemsPanelTemplate>
           <VirtualizingStackPanel
              Orientation="Horizontal" />
        </ItemsPanelTemplate>
     </GridView.ItemsPanel>
</GridView>
Run Code Online (Sandbox Code Playgroud)

但是我在选择器中给出的组是null或DependencyObject,我似乎无法获得任何数据.如果我没有得到任何信息,我怎么能做出如何改变GroupStyle的明智决定.有没有办法可以通过附属物或某些东西传递财产?

Sti*_*gar 1

基于此论坛主题,您可以通过将对象转换为 ICollectionView 并访问 .Group 属性来提取对象,您将在其中获取将组绑定到的对象。这允许对模板进行智能决策。然而,它仍然对我(或线程中的其他人)不起作用,因为尽管返回了不同的样式,但只应用了一种样式。

编辑:事实证明 GroupTemplate 并不旨在生成不同的组。其目的是更改组的视图,例如在快照视图或所有组都发生更改的类似情况下。