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

Bind to dictionary list where value is object

2 Answers 241 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
Chris
Top achievements
Rank 1
Chris asked on 25 Aug 2014, 12:40 PM
        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 Answers, 1 is accepted

Sort by
0
Accepted
Hristo Valyavicharski
Telerik team
answered on 26 Aug 2014, 12:10 PM
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.

 
0
Chris
Top achievements
Rank 1
answered on 26 Aug 2014, 12:14 PM
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.
Tags
ListBox
Asked by
Chris
Top achievements
Rank 1
Answers by
Hristo Valyavicharski
Telerik team
Chris
Top achievements
Rank 1
Share this question
or