I wanted to know how can I populate a ListBox dinamically. But I don’t want a custom class, I just want a normal selector.
I normally do something like this with my own classes:
<ListBox Name="itemList" SelectionChanged="itemList_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding title}"/>
<TextBlock Text="{Binding subtitle}" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
What I don’t know is how to simply generate the simplest thing: something like this:
<ListBox>
<ListBoxItem Name="item1" Content="First item" />
<ListBoxItem Name="item2" Content="Second item" />
<ListBoxItem Name="item3" Content="Third item" />
</ListBox>
Could you give an example of the structure? I don’t know if I have to use datatemplate or what…
Thank you
[Edited: added Name property in what I need]
You just have to set up a Collection with your items in your DataContext and set it as ListBox.ItemsSource. This will then fill in your DataTemplate.
See for example:
And:
Example source: http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.itemssource(v=vs.95).aspx
Edit: according to the discussion in the comments, maybe you only need a simple ItemsControl (as you don’t need to retain the selected item or even better to handle multiple selection, which is what the ListBox is for).
For example:
And:
And of course setting up the ButtonCommand to navigate to the URL passed in the parameter, but that depends on how you’re setting the ViewModel / bindings up.