2016-06-08 111 views
0

如何獲取(或循環)GridView中的所有項目?下面的XAML生成的可點擊我需要能夠遍歷這些元素並更新它們的值項/點擊在GridView中獲取項目Windows 8

<GridView 
    x:Name="itemGridView" 
    AutomationProperties.AutomationId="ItemsGridView" 
    AutomationProperties.Name="Items" 
    TabIndex="1" 
    Grid.RowSpan="2" 
    Padding="116,136,116,46" 
    ItemsSource="{Binding Source={StaticResource itemsViewSource}}" 
    SelectionMode="None" 
    IsSwipeEnabled="false"> 
    <GridView.ItemTemplate> 
     <DataTemplate> 
      <Grid HorizontalAlignment="Left" Width="250" Height="250" Tapped="Grid_Tapped" RightTapped="Grid_RightTapped"> 
       <Border Background="{ThemeResource ListViewItemPlaceholderBackgroundThemeBrush}"> 
        <Image Source="{Binding ImageURL}" Stretch="UniformToFill" AutomationProperties.Name="{Binding Title}"/> 
       </Border> 
       <StackPanel VerticalAlignment="Bottom" Background="{ThemeResource ListViewItemOverlayBackgroundThemeBrush}"> 
        <TextBlock Text="{Binding Name}" Foreground="{ThemeResource ListViewItemOverlayForegroundThemeBrush}" Style="{StaticResource BaseTextBlockStyle}" Height="60" Margin="15,0,15,0" FontWeight="SemiBold"/> 
        <TextBlock Text="{Binding CurrentState}" Foreground="{ThemeResource ListViewItemOverlaySecondaryForegroundThemeBrush}" Style="{StaticResource BaseTextBlockStyle}" TextWrapping="NoWrap" Margin="15,0,15,10" FontSize="12"/> 
       </StackPanel> 
      </Grid> 
     </DataTemplate> 
    </GridView.ItemTemplate> 
</GridView> 

的網格。這是我嘗試這樣做,但不能

foreach (var item in itemGridView.Items) 
{ 
    var _Container = itemGridView.ItemContainerGenerator.ContainerFromItem(item); 

    //Once i have the contrainer i know i can access its childern but this line of code throws error 
} 

enter image description here

有沒有一種辦法能夠做什麼,我想在這裏做什麼?

或者如果在Windows 8中有RowDataBound事件?

回答

0

如果您想更新值,則不需要獲取容器。例如你有一個數據類:

public class mydemotable 
{ 
    public string CurrentState { get; set; } 
    public string Name { get; set; } 
} 

這裏是例如如何通過不斷變化的名稱值的元素填補這一類環:

 mydemotable demoitem = new mydemotable 
     { 
      Name = "Skywalker", 
      CurrentState = "lalala" 
     }; 

     ObservableCollection<mydemotable> items = new ObservableCollection<mydemotable>(); 
     items.Add(demoitem); 

     itemGridView.DataContext = items; 


     foreach (mydemotable item in itemGridView.Items) 
     { 
      item.Name = "Alex"; 
     } 
+0

返回null。 – Salman