我怎样才能使地铁GridView控件组使用不同的布局?(How can I make Groups

2019-07-30 08:52发布

我正在写一个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>

但是,我在选择我给出的组为空或者说我似乎无法得到任何数据从一个DependencyObject的。 我应该如何做出明智的决定如何改变GroupStyle如果我没有获得任何信息。 有没有一种方法,我可以通过沿着这些线路的附加属性或东西传递的属性?

Answer 1:

基于这个论坛主题 ,您可以通过它铸造ICollectionView,并在访问属性。集团在那里你会得到你所绑定的组对象提取对象。 这样就可以在模板上明智的决定。 然而,它仍然不适合我(或其他人在线程),因为尽管不同风格返回的实际上只有一个样式应用于工作。

编辑:原来,GroupTemplate无意产生不同的组。 它的目的是改变该组的视图,例如在捕获的视图或其中所有基团改变类似的情况。



文章来源: How can I make Groups in a Metro GridView use different Layouts?