#960 – A ListBox Can Store Objects of Different Types
November 28, 2013 1 Comment
You’ll most often bind the contents of a ListBox to a collection of items that are all of the same type. You can, however, add different types of objects to the same ListBox. Items in a ListBox can be either standard CLR objects or objects that derive from UIElement.
In the example below, we add several different types of objects to a ListBox.
<ListBox Margin="15"> <ListBoxItem>Simple string</ListBoxItem> <local:Actor FullName="Liam Neeson" BirthYear="1952" KnownFor="Schindler's List"/> <Button Content="Click Me"/> <TextBox Text="zzz" Width="100"/> <StackPanel Orientation="Horizontal"> <TextBlock Text="Username:"/> <TextBox Width="100" Margin="5,0"/> </StackPanel> </ListBox>
This example is not very typical, but points out that you can add any type of object that you like to a ListBox. Note that the user can also select any item, whether the item is a string or a user interface element.