Search code examples
c#.netuser-interfacelistviewitem

C# ListViewItem type information suppression


I'm encountering this 'strange' (for me) behavior of the ListViewItem. each item is surrounded by the 'type' like so: ListViewItem: {value}. How to get rid of the 'encapsulation of item?

The code to put items in ListView:

if (task.Items != null)
        {
            foreach (QuestTaskItem it in task.Items)
            {
                ListViewItem vi = new ListViewItem();
                vi.Name = it.Name;
                vi.Text = it.Name;
                vi.Tag = it;

                itemList.Items.Add(vi);
            }
        }

Output: The output of the code above

UPDATE 1 QuestTaskItem:

public class QuestTaskItem
{
    public int Id;
    public string Name;
    public string Data;
    public string Image;
    public int Evalutation;

    public QuestTaskItem (int id, string name, string data, string img, int eval)
    {
        Id = id;
        Name = name;
        Data = data;
        Image = img;
        Evalutation = eval;
    }
}

Solution

  • For your checkedListBox, change itemList.Items.Add(vi); to itemList.Items.Add(vi.Text);

    A better way is to bind the CheckedListBox:

    ((ListBox)itemList).DataSource = task.Items;
    ((ListBox)itemList).DisplayMember = "Name";