This is a migrated thread and some comments may be shown as answers.

RadListBox Casting SelectedItems to Objects

3 Answers 271 Views
ComboBox and ListBox (obsolete as of Q2 2010)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Ilya
Top achievements
Rank 1
Ilya asked on 13 Mar 2008, 06:27 AM
Hi there..

I've just started using the Telerik framework, but am a little stuck on one item.
I populate a RadListBox with a collection of objects, setting the ValueMember and DisplayMember from the object - which populates perfectly well.

But when i want to cast a selected item to an object, i can't cast a RadElement to a custom object (like i normally do with stantard list box).

Is there anyway of doing this like a normal list box? - hope this makes sense.


Thanks,
ilya

3 Answers, 1 is accepted

Sort by
0
Georgi
Telerik team
answered on 17 Mar 2008, 08:24 AM
Hello Ilya,

Thank you for writing.

As our RadListBox has a more sophisticated visual appearance, RadListBoxItems are created when the control is bound to data. In fact, RadListBox can handle any instance of RadItem type as item. Currently, however, it cannot handle the original data objects internally. We intend to implement this feature in the future.

However, it is possible to assign a reference (for later use) in the RadListBoxItem.Tag property to the corresponding data object by using the RadListBox.ItemDataBound event.

Please follow this link for additional information:

http://www.telerik.com/community/forums/thread/b311D-bamhdb.aspx

I hope this helps. If you have any additional questions, please do not hesitate to write us again.

All the best,
Georgi
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Ilya
Top achievements
Rank 1
answered on 18 Mar 2008, 08:06 AM
Hi there again,

Thanks so much for the reply.
I've almost got it, BUT i think i'm missing a partof the puzzle.

I've followed what it says in the link to the post you provided. 
http://www.telerik.com/community/forums/thread/b311D-bamhdb.aspx

I found that when data-binding the RadListBox, the onItemDataBound event accepts a RadListBoxElement not a RadListBoxItem (as suggested) - is this correct?

I can pull out the object sent in via the ItemDataBoundEventArgs, and assign that to the RadListBoxElement.Tag. However, when i try and access the Tag value on the RadlistBoxItem later, it's null.
 
Is there anyway of assigning this to the Tag on the RadListBoxItem instead of the RadListBoxElement? Please see below for what i have currently coded... Something must be wrong here...


I have the following to populate the RadListBox and attach the event:

RadListBoxElement listBoxElement = (RadListBoxElement)this.lstPtyMedia.RootElement.Children[0];

listBoxElement.ItemDataBound +=

new ItemDataBoundEventHandler(onItemDataBound);

lstPtyMedia.DataSource = objPty.PropertyMediaCollectionByPropertyID;

lstPtyMedia.ValueMember =

"PropertyMediaID";

lstPtyMedia.DisplayMember =

"FileName";



The following is in  the onItemDataBound event:

private void onItemDataBound(Object sender, ItemDataBoundEventArgs e)

{

    PropertyMedia objPtyMedia = (PropertyMedia)e.DataItem;

    RadListBoxElement lstElement = (RadListBoxElement)sender;

    lstElement.Tag = objPtyMedia;

}

Thanks,  and i look forward to your replly...

Regards,
ilya.

0
Georgi
Telerik team
answered on 20 Mar 2008, 01:53 PM
Hi Ilya,

In fact, ItemDataBound event does not work exactly as you assume. Since its behavior is a bit confusing, it is well described in "Databinding" topic of RadComboBox documentation. However, let me clarify it here once more. Although the event is fired on every item that is being bound, the sender argument always points to the same RadListBoxElement (the main element in the control's hierarchy). The arguments you are need are in the ItemDataBoundEventArgs. The value of DataBoundItem property points to the actual listbox item while DataItem points to the original data object. So you only have to assign the value of DataItem to DataBoundItem.Tag property like this:

    e.DataBoundItem = e.DataItem;

If you have any other questions, please write us again.

All the best,
Georgi
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
Tags
ComboBox and ListBox (obsolete as of Q2 2010)
Asked by
Ilya
Top achievements
Rank 1
Answers by
Georgi
Telerik team
Ilya
Top achievements
Rank 1
Share this question
or