Bind to dictionary list where value is object

3 posts, 1 answers
  1. Chris
    Chris avatar
    9 posts
    Member since:
    Sep 2012

    Posted 25 Aug 2014 Link to this post

            RadListBoxDataType.DataTextField = "Value.DisplayName";
            RadListBoxDataType.DataValueField = "Key";
            RadListBoxDataType.DataSource = dataTypeList;
            RadListBoxDataType.DataBind();

    The above does not work.  What I want to know is it possible to bind to a property of "Value" where "Value" is an object.

  2. Answer
    Hristo Valyavicharski
    Admin
    Hristo Valyavicharski avatar
    975 posts

    Posted 26 Aug 2014 Link to this post

    Hi Chris,

    Handle the OnItemDataBound event and set the text of the item:

    <telerik:RadListBox runat="server" ID="RadListBoxDataType" OnItemDataBound="RadListBoxDataType_ItemDataBound"></telerik:RadListBox>

    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Dictionary<string, User> dataTypeList = new Dictionary<string, User>();
      
            User user = new User();
            user.DisplayName = "User1";
     
            dataTypeList.Add("1", user);
     
            RadListBoxDataType.DataValueField = "Key";
            RadListBoxDataType.DataSource = dataTypeList;
            RadListBoxDataType.DataBind();
     
        }
        protected void RadListBoxDataType_ItemDataBound(object sender, RadListBoxItemEventArgs e)
        {
            KeyValuePair<string, User> kvp = (KeyValuePair<string, User>) e.Item.DataItem;
            e.Item.Text = kvp.Value.DisplayName;           
        }
    }
     
    public class User
    {
        public string DisplayName { get; set; }
    }

    I hope this helps.

    Regards,
    Hristo Valyavicharski
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Chris
    Chris avatar
    9 posts
    Member since:
    Sep 2012

    Posted 26 Aug 2014 in reply to Hristo Valyavicharski Link to this post

    Thank you Hristo, I'm not sure why I didn't think fo that (thanks for replying to my support ticket about this as well)

    I had found some mentions for what I think was examples of WPF or WinForms where people were able to do something like {Binding Value.DisplayName} so I was kind of hoping for the same but your solution works as well.
Back to Top