Je développe une application Windows Phome. J'ai ListBox suivant sur une page:ListBox ItemsSource La liaison ne fonctionne pas
<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>
J'utilise ceci pour remplir la zone de liste sur l'événement Loaded:
this.WallList.ItemsSource = StartingWall.GetWallPosts();
Et maintenant, je veux ajouter d'autres éléments par programme lorsque l'utilisateur écrire du texte sur un TextBox et cliquez sur un bouton. Je veux placer ce texte sur le champ Commentaire.
Je suis en train de remplir les champs restants avec des données par défaut.
Ma question est:
Comment puis-je ajouter d'autres éléments à ListBox WallList?
Quelqu'un a suggéré de faire ce qui suit:
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}">
Mais Reliure ListBox ItemsSource ne fonctionne pas pour moi. Je suis le constructeur d'initialisation MyWallPosts
, juste avant InitializeComponent();
, comme ceci:
public Wall()
{
MyWallPosts = StartingWall.GetWallPosts();
InitializeComponent();
}
Un conseil?
Merci.