我是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种方法。
数据绑定,在这种情况下,您的列表框的ItemsSource属性将绑定到某些List / enumerable。并将属性绑定到image和textblock控件。
将项目直接添加到xaml的列表框中(或者可以在后面的代码中完成)
从代码示例中,您仅创建了列表框项目模板。它不会显示任何内容。您需要创建项目源并将其绑定到列表框。
如果您有静态列表,并且想要在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>
下面是绑定列表的完整列表框示例。
您的listBox xaml中的网格。
<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 class Car {public string Model {get; 放; }公用字串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;
希望这可以帮助
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句