在GridView Windows 8中获取项目

时间:2016-06-08 08:55:11

标签: gridview windows-8 windows-store-apps

如何获取(或循环)gridview中的所有项目?以下XAML生成可点击/可点击的项目网格

<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的事件?

1 个答案:

答案 0 :(得分:0)

如果您想更新值,则不需要获取容器。例如,您有一个数据类:

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

以下是如何填充此类并通过更改Name值来循环遍历元素的示例:

     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"; 
        }