12

私はWindows8Metroアプリを書いています。3つのグループでGridViewを描画しようとしています。それらのグループの1つに、他のグループとは異なる方法でアイテムをレイアウトしてもらいたいです。以前に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 プロパティにアクセスすることで、オブジェクトを抽出できます。これにより、テンプレートに関するインテリジェントな決定が可能になります。ただし、異なるスタイルが返されるにもかかわらず、1 つのスタイルしか適用されないため、私 (またはスレッド内の他の人) にはまだ機能しません。

編集: GroupTemplate は異なるグループを生成することを意図していないことがわかりました。グループのビューを変更することを目的としています。たとえば、スナップ ビューや、すべてのグループが変更される同様のケースです。

于 2012-11-13T14:49:00.150 に答える