如何解决问题如下:
当我drawItem,然后得到消息错误:“无法将'System.Collections.Generic.List`1[mypro.InfoDialog+Mycontact]'类型的对象转换为'Mycontact'。”
行号处的 C# 代码:
public class Mycontact
{
public string P_DISPLAY_NAME { get; set; }
public string P_AVAILABILITY { get; set; }
public string P_AVATAR_IMAGE { get; set; }
}
Mycontact fbContact;
private void AddDataToList()
{
var fbList = new List<Mycontact>();
foreach (dynamic item in result.data)
{
fbContact = new Mycontact() { P_DISPLAY_NAME = (string)item["name"], P_AVAILABILITY = (string)item["online_presence"]};
fbList.Add(fbContact);
listBox1.Items.Add(fbList);
}
}
private int mouseIndex = -1;
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
if (e.Index == -1) return;
line number:
Mycontact contact = (Mycontact)listBox1.Items[e.Index];
Brush textBrush = SystemBrushes.WindowText;
if (e.Index > -1)
{
// Drawing the frame
if (e.Index == mouseIndex)
{
e.Graphics.FillRectangle(SystemBrushes.HotTrack, e.Bounds);
textBrush = SystemBrushes.HighlightText;
}
else
{
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{
e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds);
textBrush = SystemBrushes.HighlightText;
}else{
e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds);
}
// Drawing the text
e.Graphics.DrawString(contact.P_DISPLAY_NAME, e.Font, textBrush, e.Bounds.Left + 20, e.Bounds.Top);
}
}
}