简体   繁体   English

列表框无法显示在WP8仿真器中

[英]Listbox cannot display in WP8 Emulator

It's wired. 它是有线的。 I'm developing a WP8 app. 我正在开发WP8应用程序。 the XAML code is: XAML代码是:

<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: CS代码为:

 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. Windows Phone 8模拟器中不显示列表框中的内容。 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> 由ItemSource决定<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. 通常,将DataContext设置为ViewModel,然后将ItemsSource设置为ViewModel内部的集合/列表。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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