Tuesday, December 03, 2013

#960 – A ListBox Can Store Objects of Different Types

Inline image 1


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>