我尝试使用数据绑定在WP7中添加到ListBox,使用C#。但它似乎不起作用,我完全不知道为什么。只绘制了行的边框,并且文本的内容不会显示。
这是我要添加到
的课程 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>
答案 0 :(得分:0)
您应该使用属性ObservableCollection
作为绑定资源而不是列表或实现INotifyPropertyChanged
。