列表框无法在WP8模拟器中显示

时间:2014-11-13 03:14:37

标签: c# wpf windows-phone-7 windows-phone-8 listbox

有线。我正在开发WP8应用程序。 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>

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

列表框中的内容无法在Windows Phone 8模拟器中显示。 我想我的代码中没有任何错误,这很简单,但我没有&#39;知道原因

2 个答案:

答案 0 :(得分:0)

它是确定<ListBox.ItemTemplate>

输出的ItemsSource
this.ResultBox.ItemsSource = results;

通常将DataContext设置为ViewModel,然后将ItemsSource设置为ViewModel内的集合/列表。

答案 1 :(得分:0)

您也可以在ItemsSource="{Binding}"

中设置ListBox