chr*_*son 12 c# microsoft-metro windows-runtime
我正在写一个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"];
}
}
Run Code Online (Sandbox Code Playgroud)
所以我改编/扩展了适合我的东西:
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" );
}
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
但是我在选择器中给出的组是null或DependencyObject,我似乎无法获得任何数据.如果我没有得到任何信息,我怎么能做出如何改变GroupStyle的明智决定.有没有办法可以通过附属物或某些东西传递财产?