0

I have ListView that is dynamic and Columns name need to change dynamic so i create this:

var gridView = new GridView();
            this.lvWorkers.View = gridView;
            foreach(string c in columnName)
            {
                gridView.Columns.Add(new GridViewColumn
                {
                    Header = c.Remove(0, 5),
                    DisplayMemberBinding = new Binding(c)
                });
            }

Now i want add Items to this list but there is no SubItems like in Win Forms. I find similar problems, the answer was to make class that is defined. Add Items to Columns in a WPF ListView I need to add Items dynamic.
Win Forms which is not work.

foreach (OneStudentEvent e in oneEventList)
{
     ListViewItem item = new ListViewItem(e.Indeks.ToString());
     item.SubItems.Add(e.eventString);
     ...
     lvWorkers.Items.Add(item);
}

EDIT

Now i remove DisplayMemberBinding = new Binding(c)

and add:

lvWorkers.ItemsSource = getList();   

private ArrayList getList()
    {
        ArrayList data = new ArrayList();
        for(int i=0; i<20; i++)
        {
            List<string> tempList = new List<string>();
            for(int j = 0; j < 30; j++)
            {
                tempList.Add(j.ToString());
            }
            data.Add(tempList);
        }
        return data;
    }

And i don't see string in list but word: (Collection). I know that this is my bad to show collection no single string, but i don't know hot to make it.

7
  • You are trying to work with ListView in wpf is you would do in winforms. Please don't. Use ItemsSource, bindings, data templating and all nice WPF features instead. See e.g. this (as an example of dealing with sub-items). Commented May 31, 2016 at 14:47
  • I'm searching of solution, and try to find how to use this <code>ItemSource</code>, but i only find static solution like: dotnetperls.com/listview-wpf , wpf-tutorial.com/listview-control/… Commented Jun 1, 2016 at 12:37
  • Now i Edit my question, and try to do sth with ItemsSource. Commented Jun 1, 2016 at 12:48
  • You have to use DisplayMemberBinding (or some other way to bind columns, but lets stick to simplest solution) to bind to subitem text (using e.g. Path=[0] to bind to first subitem). Normally (using MVVM) you would create a type to hold values per column as properties and then bind to those properties by name. Commented Jun 1, 2016 at 14:09
  • In my edit I use 30 columns, but it change dynamic, so i if need make a type which would hold values per column i need to create dynamic class with Dictionary ? I'm newbie at WPF, for me the best way for explain, and learn are examples (if i found example, it is with static class - Person: Id, Name, Last Name etc) Btw really thanks for every help :) Example of static: stackoverflow.com/questions/15865829/… stackoverflow.com/questions/12828704/… Commented Jun 1, 2016 at 15:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.