我尝试使用 C# 使用数据绑定添加到 WP7 中的 ListBox。但它似乎不起作用,我完全不知道为什么。仅绘制行的边框并且不显示文本的内容。
这是我要添加的课程
public class ResultTemplate
{
public string Mealname;
public string CanteenID;
public DateTime Date;
}
public class View
{
List<ResultTemplate> result;
//....
void Load()
{
this.listbox.ItemSource = result;
}
}
我的 XAML 代码:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ListBox x:Name="searchlist" ItemsSource="{Binding}" SelectionChanged="searchlist_SelectionChanged" >
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderThickness="1" BorderBrush="#FF807C7C" Width="500" HorizontalAlignment="Center">
<StackPanel MinHeight="60" Width="450" HorizontalAlignment="Left" Margin="25,0,0,0">
<StackPanel Orientation="Horizontal">
<TextBlock Name="textblock1" Text="{Binding Mealname}" HorizontalAlignment="Left" TextWrapping="Wrap" FontSize="25" Width="385" />
<!--<Image Source="{Binding Image}" Stretch="None" Margin="10,0,0,0" Height="50" Width="50"/>-->
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Name="textblock2" Text="{Binding CanteenID}" HorizontalAlignment="Left" Width="150" FontSize="20" Foreground="#FF004AFF"/>
<TextBlock Name="textblock3" Text="{Binding Date,StringFormat=dddd.dd/MM/yyyy}" HorizontalAlignment="Left" FontSize="20" Foreground="#FF00AB00"/>
</StackPanel>
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>