Vincular diccionario de tipo complejo a RadioButtonList

No estoy seguro de si esto es posible, pero tengo un diccionario de un tipo complejo y quiero vincularlo al control RadioButtonList/CheckBoxList:

public class ComplexType
{
   public String Name { get; set; }
   public String FormattedName { get; set; }
   public String Description { get; set; }
}
var listOfMyTypes = new Dictionary<int, ComplexType>();

var myType = new ComplexType { Name = "Name", Description = "Description", FormattedName = "Name|Description" };
var myType1 = new ComplexType { Name = "Name2", Description = "Description2", FormattedName = "Name|Description2" };


listOfMyTypes.Add(1, myType);
listOfMyTypes.Add(2, myType1);

m_dropDownlist.DataTextField = "Value"; //What do I put here to render "Name"
m_dropDownlist.DataValueField = "Key";
m_dropDownlist.DataSource = listOfMyTypes;
m_dropDownlist.DataBind();

preguntado el 30 de junio de 12 a las 14:06

3 Respuestas

Intenta enlazar a Dictionary.Values en lugar de esto:

m_dropDownlist.DataTextField = "Name"
m_dropDownlist.DataValueField = "Description";
m_dropDownlist.DataSource = listOfMyTypes.Values;
m_dropDownlist.DataBind();

Respondido el 30 de junio de 12 a las 14:06

Esto es exactamente lo que estaba buscando, solo necesitaba un segundo par de ojos. ¡Gracias! - stefano d

Establece TextField en la propiedad que desea mostrar. Establece ValueField en la propiedad que desea publicar. Finalmente, enlace a los valores del diccionario. Estoy buscando una versión de "plantilla" de DropDownList, pero no encuentro ninguna. Entonces, si necesita la clave del diccionario, es posible que deba hacerlo de la manera más difícil: repetir / agregar.

m_dropDownlist.DataTextField = "FormattedName"; //What do I put here to render "Name" 
m_dropDownlist.DataValueField = "Name"; 
m_dropDownlist.DataSource = listOfMyTypes.Values; 
m_dropDownlist.DataBind(); 

Encontré otra manera. Puede anular "ToString ()" en su clase:

public class ComplexType      
{      
   public String Name { get; set; }      
   public String FormattedName { get; set; }      
   public String Description { get; set; } 
   public override string ToString()
   {
      return this.Name;
   }     
}  

Luego vincular normalmente:

m_dropDownlist.DataTextField = "Value"; //What do I put here to render "Name" 
m_dropDownlist.DataValueField = "Key"; 
m_dropDownlist.DataSource = listOfMyTypes; 
m_dropDownlist.DataBind(); 

Respondido el 30 de junio de 12 a las 15:06

Puedes escribir

m_dropDownlist.DataTextField = "Value.Name";

Respondido el 25 de enero de 13 a las 14:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.