user
user

Reputation: 55

convert generic type to listbox control

I'm using silverlight framework 4: I'm trying to list my items in a generic list to a listbox control: But the only data a receive is the classname itself.

lsBox => the listbox control lsTags => generic type

My question is: how can I add my items in the generic list, to the listbox control?

my code is:

 lsBox.ItemsSource = lsTags;

Upvotes: 2

Views: 435

Answers (2)

Bounz
Bounz

Reputation: 882

You can use DisplayMemberPath and SelectedValuePath properties of your ListBox control to tell ListBox which property's value should be displayed for every item and which property should be used for determening ListBox.SelectedValue property. Or use ListBox.ItemTemplate to display a complex data like this:

<ListBox x:Name="usersInGroupLBox">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <CheckBox IsChecked="{Binding IsActive, Mode=TwoWay}" />
                <TextBlock Text="{Binding User.UserName}" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Do not forget, you can use only public properties for binding. Check you class Tag.

Upvotes: 2

svick
svick

Reputation: 244998

The default behavior of ListBox (and most other controls) for displaying user types is to call the ToString() method. The default behavior of that is to display the class name.

What you should do depends on what you want to display, but if it's something simple like displaying the value of the Name property, just set the DisplayMemberPath property:

<ListBox Name="lsBox" DisplayMemberPath="Name" />

Upvotes: 0

Related Questions