2012-07-10 39 views
12

我在写一个Windows 8 Metro应用程序。我试图绘制一个带有三个组的GridView。我希望这些团队中的一个以不同于其他人的方式布置他们的物品。我之前在WPF中使用过选择器,所以我认为这将是一条很好的路线。所以我尝试了GroupStyleSelector我发现这个example on MSDN如何让Metro GridView中的组使用不同的布局?

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 tha我似乎无法获取任何数据。如果我没有给出任何信息,我该如何做出如何改变GroupStyle的明智决定。有没有一种方法可以通过附属资产或其他方式传递资产?

回答

1

基于此论坛thread您可以通过将对象转换为ICollectionView并访问.Group属性来获取您绑定组的对象。这允许在模板上做出明智的决定。但是它仍然不适用于我(或线程中的其他人),因为尽管返回了不同的样式,但只应用了一种样式。

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

相关问题