私は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を変更する方法についてインテリジェントな決定を行うにはどうすればよいですか。アタッチされたプロパティまたはそれらの線に沿った何かを介してプロパティを渡す方法はありますか?