我正在开发Windows Phome应用程序。我有一个页面上的以下列表框:ListBox ItemsSource绑定不起作用
<ListBox Margin="10,10,8,8" x:Name="WallList">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid x:Name="ListBoxItemLayout" Background="Transparent" Margin="10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.33*"/>
<ColumnDefinition Width="0.77*"/>
</Grid.ColumnDefinitions>
<Image HorizontalAlignment="Left" Margin="0" Source="{Binding ImagePath}" Height="200"/>
<StackPanel Margin="5,0,0,0" Grid.Column="1">
<TextBlock x:Name="Name" TextWrapping="Wrap" Text="{Binding Name}" Style="{StaticResource PhoneTextTitle2Style}"/>
<TextBlock x:Name="Comment" Margin="0,5,0,0" TextWrapping="Wrap" Text="{Binding Comment}" Style="{StaticResource PhoneTextNormalStyle}" Height="130"/>
<TextBlock x:Name="When" TextWrapping="Wrap" Text="{Binding When}" Style="{StaticResource PhoneTextTitle3Style}" VerticalAlignment="Bottom"/>
</StackPanel>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
我用这个来填补Loaded事件列表框:
this.WallList.ItemsSource = StartingWall.GetWallPosts();
,现在我想以编程方式添加更多的项目,当用户写下来TextBox上的一些文本并单击按钮。我想把这个文本放在评论栏里。
我打算用默认数据填充其余字段。
我的问题是:
如何添加更多项目到WallList ListBox?
有人建议做到以下几点:
public ObservableCollection<WallPostEntry> MyWallPosts {get;set;}
// Initialize MyWallPosts to whatever
MyWallPosts.Add(new WallPostEntry("new entry"));
<ListBox Margin="10,10,8,8" x:Name="WallList" ItemsSource="{Binding MyWallPosts}">
但是绑定列表框的ItemsSource不会为我工作。我在构造函数初始化MyWallPosts
,只是InitializeComponent();
之前,像这样:
public Wall()
{
MyWallPosts = StartingWall.GetWallPosts();
InitializeComponent();
}
有什么建议?
谢谢。