我在寫一個Windows 8 Metro應用程序。我試圖繪製一個帶有三個組的GridView。我希望這些團隊中的一個以不同於其他人的方式佈置他們的物品。我之前在WPF中使用過選擇器,所以我認爲這將是一條很好的路線。所以我嘗試了GroupStyleSelector我發現這個example on MSDN:如何讓Metro GridView中的組使用不同的佈局?
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 tha我似乎無法獲取任何數據。如果我沒有給出任何信息,我該如何做出如何改變GroupStyle的明智決定。有沒有一種方法可以通過附屬資產或其他方式傳遞資產?