#960 – A ListBox Can Store Objects of Different Types

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>

960-001
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.

Advertisements

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

One Response to #960 – A ListBox Can Store Objects of Different Types

  1. Pingback: Dew Drop – December 2, 2013 (#1674) | Morning Dew

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: