简体   繁体   中英

Listbox cannot display in WP8 Emulator

It's wired. I'm developing a WP8 app. the XAML code is:

<phone:PhoneApplicationPage
    x:Class="WPTestService4DotNet.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
            <TextBlock x:Name="PageTitle" Margin="9,-7,0,0" Style="{StaticResource PhoneTextNormalStyle}"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <TextBlock Grid.Row="0" Text="avatar" FontSize="18"></TextBlock>

            <ListBox Grid.Row="1" x:Name="ResultBox" BorderBrush="Bisque" BorderThickness="4" DataContext="{Binding}">
               <ListBox.ItemTemplate>
                   <DataTemplate>
                       <StackPanel>
                            <TextBlock Text="inner display" FontSize="18"></TextBlock>
                            <TextBlock Text="{Binding Path=Id}" FontSize="18" FontStyle="Italic" TextWrapping="Wrap"></TextBlock>
                            <TextBlock Text="{Binding Path=Title}" FontSize="18" TextWrapping="Wrap"></TextBlock>
                        </StackPanel>
                   </DataTemplate>
               </ListBox.ItemTemplate>
            </ListBox>
        </Grid>
    </Grid>

</phone:PhoneApplicationPage>

and the CS code is:

 public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            var results = new List<Result>();
            results.Add(new Result() { Id = "1", Title = "first" });
            results.Add(new Result() { Id = "2", Title = "second" });
            results.Add(new Result() { Id = "3", Title = "third" });
            this.DataContext = results;
            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();
        }
        public class Result
        {
            public string Id { set; get; }
            public string Title { set; get; }
        }
}

Content in the listbox doesn't display in windows phone 8 emulator. I guess no error in my code it's so simple , but I dont' know why

It's the ItemsSource that determines the output from the <ListBox.ItemTemplate>

this.ResultBox.ItemsSource = results;

Generally you set the DataContext to the ViewModel, then the ItemsSource to a collection/list that is inside the ViewModel.

您也可以将ItemsSource="{Binding}"设置ItemsSource="{Binding}" ListBox

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM