列表框数据&图像未显示在Windows Phone 8中

时间:2014-05-31 08:38:51

标签: windows-phone-8 listbox

我在Windows Phone 8上是全新的。我想在列表框中一起显示图像和文本。我使用流动的代码来做到这一点,但它在我的设计视图中没有显示任何内容。然后我调试这个代码除了页面标题以外什么也没显示。请帮我 。

 <phone:PhoneApplicationPage
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"
xmlns:system="clr-namespace:System;assembly=mscorlib"
xmlns:UI="clr-namespace:Microsoft.Advertising.Mobile.UI;assembly=Microsoft.Advertising.Mobile.UI"
x:Class="Masala.Infotainment"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d"
shell:SystemTray.IsVisible="True">

<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <Grid.Background>
        <ImageBrush Stretch="Fill" ImageSource="images/home_bg.png"/>
    </Grid.Background>

    <StackPanel Grid.Row="0" Margin="12,17,0,28">
        <TextBlock Text="Page Title"></TextBlock>
    </StackPanel>

    <Grid Grid.Row="1">
        <ListBox Name="mahin" Width="450">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Image Margin="8"
                                VerticalAlignment="Top"
                                Source="images/cc.png"
                                Width="100"
                                Height="100" />
                        <StackPanel>
                            <TextBlock Text="MAHIN"></TextBlock>
                        </StackPanel>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Grid>

2 个答案:

答案 0 :(得分:0)

您可以使用的方法之一:

XAML:

<ListBox Name="lstView" ItemsSource="{Binding}">
   <ListBox.ItemTemplate>
      <DataTemplate>
          <StackPanel Orientation="Horizontal">
             <Image Source="{Binding ImagePath}"></Image>
             <TextBlock Text="{Binding Name}"></TextBlock>
        </StackPanel>
      </DataTemplate>
   </ListBox.ItemTemplate>
</ListBox>

C#:

public class Article
{
    public string Name { get; set; }

    public string ImagePath { get; set; }
}


 Article article1 = new Article() { Name = "name1", ImagePath = "path of image 1" };
 Article article2 = new Article() { Name = "name2", ImagePath = "path of image 2" };

 var articles = new List<Article>();
 articles.Add(article1);
 articles.Add(article2);

 lstView.DataContext = articles;

答案 1 :(得分:0)

您希望如何在列表框中显示图像和文本?有两种方式。

  1. 数据绑定,在这种情况下,您的列表框的ItemsSource属性将绑定到某些List / enumerable。并且属性绑定到图像和文本块控件。

  2. 将项目直接添加到xaml中的列表框中(或者可以在后面的代码中完成)

  3. 从您的代码示例中,您只创建了列表框项模板。这不会显示任何东西。您需要创建项目源并将其绑定到列表框。

    如果你有静态列表并想在xaml中添加/显示它们。这是你如何做到的。

    <ListBox Grid.Row="1" width="420">
    <ListBox.Items>
                <ListBoxItem >
                    <StackPanel Orientation="Horizontal">
                    <Image Source="Assets/book1.png"></Image>
                    <TextBlock Text="Book 1" Margin="10,0,0,0" FontSize="30"></TextBlock>
                    </StackPanel>
                </ListBoxItem>
                <ListBoxItem >
                    <StackPanel Orientation="Horizontal">
                    <Image Source="Assets/book2.png"></Image>
                    <TextBlock Text="Book 2" Margin="10,0,0,0" FontSize="30"></TextBlock>
                    </StackPanel>
                </ListBoxItem>
    </ListBox.Items>
    </ListBox>
    

    以下是bindig列表的完整列表框示例。

    你的listBox xaml in gird。

    <ListBox Name="lstView" ItemsSource="{Binding}" Margin="0,160,0,0">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Image Source="{Binding Image}"></Image>
                        <TextBlock Text="{Binding Make}"></TextBlock>
                        <TextBlock Text="{Binding Model}"></TextBlock>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    

    在项目中添加新课程...
        公共类汽车     {         public string Model {get;组; }         public string Make {get;组; }

        public string Image { get; set; }
    }
    

    在xaml页面代码后面添加以下代码(页面构造函数)

     List<Car> cars = new List<Car>();
    
            var car = new Car() { Make = "Toytoa", Model = "Corola", Image = "Assets/applogo240.png" };
            cars.Add(car);
            car = new Car() { Make = "Honda", Model = "Acord", Image = "Assets/applogo240.png" };
            cars.Add(car);
            car = new Car() { Make = "Honda", Model = "Civic", Image = "Assets/applogo240.png" };
            cars.Add(car);
    
            ///Bind list to listbox..
            lstView.ItemsSource = cars;
    

    希望这有帮助