我正在编写一个 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 做出明智的决定。有没有办法可以通过附加属性或类似的方式传递属性?