0

I know this question sounds trivial but since I am new to windows phone development i cannot seem to understand how to do this in my scenario.

I have a

List<Forecast> forecasts

which i want to connect to my 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>

In code behind

WeatherInfoLowBar.DataContext = wio.forecasts;

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

1 回答 1

2

我会像这样构造 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<>用作:ItemsSourceListBox

myListBox.ItemsSource = wio.forecasts;
于 2013-10-20T13:32:37.513 回答