RadListBox Casting SelectedItems to Objects

4 posts, 0 answers
  1. Ilya
    Ilya avatar
    4 posts
    Member since:
    Feb 2008

    Posted 13 Mar 2008 Link to this post

    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
  2. Georgi
    Admin
    Georgi avatar
    212 posts

    Posted 17 Mar 2008 Link to this post

    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
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Ilya
    Ilya avatar
    4 posts
    Member since:
    Feb 2008

    Posted 18 Mar 2008 Link to this post

    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.

  5. Georgi
    Admin
    Georgi avatar
    212 posts

    Posted 20 Mar 2008 Link to this post

    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
Back to Top