2013-10-16 27 views
1

對於WinRT應用程序,我有一個GridView,其中的項目被分組,以便每個組包含一個標題和他的組元素。如何選擇分組GridView的所有項目

我想要一個應用程序欄中的按鈕,將我gridView中的所有項目傳遞到選定狀態(帶有紫​​色邊框和複選框,就像我右鍵單擊某個項目時一樣)。

我嘗試將每個項目添加到我的GridView的SelectedItems列表中,但它什麼都不做。

private void FavoriButton_Click_1(object sender, RoutedEventArgs e) 
    { 
       foreach (Categorie cat in coll) 
        itemGridView.SelectedItems.Add(cat); 

    } 

有誰知道我怎麼能把所有我的網格視圖的項目到selectedState(帶有紫色邊框和複選框)?

下面是代碼

public sealed partial class HomePage : LayoutAwarePage 
    { 
    ObservableCollection<Categorie> coll = new ObservableCollection<Categorie>(); 

    public HomePage() 
    { 

     this.InitializeComponent(); 

     cvs1.Source = coll; 

     (itemGridView as ListViewBase).ItemsSource = this.cvs1.View.CollectionGroups; 

    } 

    async private void FillPage() 
    { 


      var categories = App.api.Categories_Get(); 

      if (categories == null || categories.Count == 0) 
       return; 


      for (var i = 0; i < categories.Count; i++)      
       coll.Insert(i, categories[i]); 


    } 

     private void FavoriButton_Click_1(object sender, RoutedEventArgs e) 
    { 
       foreach (Categorie cat in coll) 
       { 
        itemGridView.SelectedItems.Add(cat); 
       } 
    } 

等樂XAML

<common:LayoutAwarePage 
x:Class="NMA.Pages.HomePage" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="using:NMA" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
xmlns:common="using:NMA.Common" 
mc:Ignorable="d"> 

<common:LayoutAwarePage.Resources> 

<CollectionViewSource x:Name="cvs1" ItemsPath="listArt" IsSourceGrouped="True" /> 

<DataTemplate x:Key="Standard250x250ItemTemplatePerso"> 
     <Grid HorizontalAlignment="Left" Width="270" Height="210" VariableSizedWrapGrid.ColumnSpan="1" VariableSizedWrapGrid.RowSpan="1" local:Tilt.IsTiltEnabled="False" > 

      <Image Width="270" Height="210" Source="{Binding ImgArt}" CacheMode="BitmapCache" VerticalAlignment="Top"/> 

     </Grid> 
    </DataTemplate> 


</common:LayoutAwarePage.Resources> 
<Grid Background="Transparent" x:Name="MyGrid"> 
    <Grid x:Name="NormalGrid"> 
     <Grid.RowDefinitions> 
     <RowDefinition Height="60"/> 
     <RowDefinition Height="*"/> 
     </Grid.RowDefinitions> 

<local:VariableGridView 
     x:Name="itemGridView" 
     AutomationProperties.AutomationId="ItemGridView" 
     AutomationProperties.Name="Items" 
     Grid.RowSpan="2" 
     Padding="120,130,120,74"   
     ItemsSource="{Binding Source={StaticResource cvs1}}" 
     ItemTemplate="{StaticResource Standard250x250ItemTemplatePerso}" 
     IsSwipeEnabled="False" 
     IsItemClickEnabled="True" 
     Background="Transparent" 
       ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.HorizontalScrollMode="Disabled" SelectionMode="Multiple"> 



        <local:VariableGridView.ItemsPanel > 
         <ItemsPanelTemplate> 
          <VirtualizingStackPanel Orientation="Horizontal" Background="Transparent" local:Tilt.IsTiltEnabled="False" Margin="0,0,100,0" /> 
         </ItemsPanelTemplate> 
        </local:VariableGridView.ItemsPanel> 
        <local:VariableGridView.GroupStyle> 
         <GroupStyle > 
          <GroupStyle.HeaderTemplate> 
           <DataTemplate x:Name="MyDataTemplate"> 
            <Button x:Name="HeaderButton" AutomationProperties.Name="MyHeaderButton" Click="HeaderButton_Click_1" Style="{StaticResource ButtonHeader_Style}" Content="{Binding NomCat}" FontSize="26" FontFamily="{StaticResource SegoeWPLight}" Margin="-24,0,0,20" Width="900" Background="Transparent"> 
            </Button> 
           </DataTemplate> 
          </GroupStyle.HeaderTemplate> 
          <GroupStyle.Panel> 


           <ItemsPanelTemplate> 
            <VariableSizedWrapGrid ItemWidth="270" ItemHeight="210" Orientation="Vertical" Margin="0,0,-30,0" MaximumRowsOrColumns="4" Background="Transparent" Width="900"> 
           </ItemsPanelTemplate> 
          </GroupStyle.Panel> 
         </GroupStyle> 
        </local:VariableGridView.GroupStyle> 
       </local:VariableGridView> 
    </Grid> 
</common:LayoutAwarePage> 

非常感謝

回答

3

我居然找到了,我努力在整個視覺樹去,而這是ItemContainerGenerator簡單。

private void FavoriButton_Click_1(object sender, RoutedEventArgs e) 
    {  
     for(var i = 0 ; i<itemGridView.Items.Count ; i++) 
     { 
      (itemGridView.ItemContainerGenerator.ContainerFromIndex(i) as GridViewItem).IsSelected = true; 
     } 
    } 

相當簡單。

相關問題