2012-07-10 37 views
12

我在寫一個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的明智決定。有沒有一種方法可以通過附屬資產或其他方式傳遞資產?

回答

1

基於此論壇thread您可以通過將對象轉換爲ICollectionView並訪問.Group屬性來獲取您綁定組的對象。這允許在模板上做出明智的決定。但是它仍然不適用於我(或線程中的其他人),因爲儘管返回了不同的樣式,但只應用了一種樣式。

編輯:事實證明GroupTemplate不打算產生不同的組。它旨在改變組的視圖,例如在快照視圖中或所有組發生更改的類似情況。

相關問題