如何在网格中显示对象列表

时间:2013-10-20 12:36:07

标签: xaml windows-phone-7

我知道这个问题听起来微不足道,但由于我是Windows手机开发的新手,我似乎无法理解如何在我的场景中这样做。

我有一个

List<Forecast> forecasts

我想连接到我的WeatherInfoBar

<Grid x:Name="WeatherInfoLowBar" Height="300" VerticalAlignment="Bottom">
    <StackPanel Grid.Column="1" VerticalAlignment="Top" Margin="0,0,0,0">
        <StackPanel Grid.Row="4" Height="40" Orientation="Horizontal" Margin="0,0,0,0">
            <TextBlock Text="Date" FontSize="22" TextAlignment="Left" Width="170"/>
            <TextBlock Text="FC" FontSize="22" TextAlignment="Left" Width="60"/>
            <TextBlock Text="Max" FontSize="22" TextAlignment="Right" Width="60"/>
            <TextBlock Text="Min" FontSize="22" TextAlignment="Right" Width="90"/>
        </StackPanel>
        <Grid >
            <StackPanel Height="40" Orientation="Horizontal" Margin="0,10,0,0">
                <TextBlock Text="{Binding date}" FontSize="22" TextAlignment="Left" Width="150"/>
                <TextBlock Text="   " FontSize="20"/>
                <Image Source="{Binding weatherIconUrl}" Width="40" Height="40"/>
                <TextBlock Text="   " FontSize="20"/>
                <TextBlock Text="{Binding tempMaxC, StringFormat='\{0\} °C'}" FontSize="22" TextAlignment="Right" Width="70"/>
                <TextBlock Text="   " FontSize="20"/>
                <TextBlock Text="{Binding tempMinC, StringFormat='\{0\} °C'}" FontSize="22" TextAlignment="Right" Width="70"/>
            </StackPanel>
        </Grid>
    </StackPanel>
</Grid>

在代码背后

WeatherInfoLowBar.DataContext = wio.forecasts;

for (int i = 0; i < wio.forecasts.Count(); i++ )
            // what code goes in here ???

1 个答案:

答案 0 :(得分:1)

我会像这样构建XAML:

<Grid x:Name="WeatherInfoLowBar" Height="300" VerticalAlignment="Bottom">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="170"/>
        <ColumnDefinition Width="60"/>
        <ColumnDefinition Width="60"/>
        <ColumnDefinition Width="90"/>
    </Grid.ColumnDefinitions>
    <TextBlock Grid.Column="0" Text="Date" FontSize="22" TextAlignment="Left" Width="170"/>
    <TextBlock Grid.Column="1" Text="FC" FontSize="22" TextAlignment="Left" Width="60"/>
    <TextBlock Grid.Column="2" Text="Max" FontSize="22" TextAlignment="Right" Width="60"/>
    <TextBlock Grid.Column="3" Text="Min" FontSize="22" TextAlignment="Right" Width="90"/>
    <ListBox Name="myListBox>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="170"/>
                        <ColumnDefinition Width="60"/>
                        <ColumnDefinition Width="60"/>
                        <ColumnDefinition Width="90"/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Text="{Binding date}" FontSize="22" TextAlignment="Left" Width="150"/>
                    <Image Source="{Binding weatherIconUrl}" Width="40" Height="40"/>
                    <TextBlock Text="{Binding tempMaxC, StringFormat='\{0\} °C'}" FontSize="22" TextAlignment="Right" Width="70"/>
                    <TextBlock Text="{Binding tempMinC, StringFormat='\{0\} °C'}" FontSize="22" TextAlignment="Right" Width="70"/>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

然后,您可以将List<>用作ItemsSource的{​​{1}}:

ListBox