Populate ListBoxItem Content
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=开发者_如何学C"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:
<Grid>
<Grid.Resources>
<src:Customers x:Key="customers"/>
</Grid.Resources>
<ListBox ItemsSource="{StaticResource customers}" Width="250" Margin="0,5,0,10"
DisplayMemberPath="LastName"/>
</Grid>
And:
public class Customer
{
public String FirstName { get; set; }
public String LastName { get; set; }
public String Address { get; set; }
public Customer(String firstName, String lastName, String address)
{
this.FirstName = firstName;
this.LastName = lastName;
this.Address = address;
}
}
public class Customers : ObservableCollection<Customer>
{
public Customers()
{
Add(new Customer("Michael", "Anderberg",
"12 North Third Street, Apartment 45"));
Add(new Customer("Chris", "Ashton",
"34 West Fifth Street, Apartment 67"));
Add(new Customer("Cassie", "Hicks",
"56 East Seventh Street, Apartment 89"));
Add(new Customer("Guido", "Pica",
"78 South Ninth Street, Apartment 10"));
}
}
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:
<ItemsControl ItemsSource="{Binding NavigateItems}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Content="{Binding Label}"
Command="{Binding ButtonCommand}"
CommandParameter="{Binding URL}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
And:
public class NavigateItem
{
public String Label { get; set; }
public String URL { get; set; }
public NavigateItem(String label, String url)
{
this.Label = label;
this.URL = url;
}
}
public class NavigateItems : ObservableCollection<NavigateItem>
{
public NavigateItems()
{
Add(new NavigateItem("Google", "http://www.google.com");
Add(new NavigateItem("Bing", "http://www.bing.com");
}
}
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.
If you don't have or want a custom class, i.e. if you just have a collection of strings, then you can assign that string collection (List, IEnumerable, string[], etc.) as the ItemsSource for the ListBox
in your code-behind (or bind it from the XAML) and it will use a TextBlock
to render that content within each ListBoxItem
.
精彩评论