12

我正在编写一个 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"];
  }
}

所以我从适合我的东西改变/扩展了它:

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" );
  }
}

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>

但是我在选择器中给出的组是 null 或 DependencyObject,我似乎无法获取任何数据。如果我没有得到任何信息,我应该如何就如何更改 GroupStyle 做出明智的决定。有没有办法可以通过附加属性或类似的方式传递属性?

4

1 回答 1

1

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

编辑:事实证明 GroupTemplate 并不打算产生不同的组。它旨在更改组的视图,例如在所有组更改的快照视图或类似情况下。

于 2012-11-13T14:49:00.150 回答