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.

Community
  • 1
  • 1
zdunek25
  • 5
  • 4
  • You are trying to work with `ListView` in [tag:wpf] is you would do in [tag:winforms]. Please don't. Use `ItemsSource`, bindings, data templating and all nice WPF features instead. See e.g. [this](http://stackoverflow.com/a/3152212/1997232) (as an example of dealing with *sub-items*). – Sinatr May 31 '16 at 14:47
  • I'm searching of solution, and try to find how to use this ItemSource, but i only find static solution like: http://www.dotnetperls.com/listview-wpf , http://www.wpf-tutorial.com/listview-control/listview-data-binding-item-template/ – zdunek25 Jun 01 '16 at 12:37
  • Now i Edit my question, and try to do sth with ItemsSource. – zdunek25 Jun 01 '16 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. – Sinatr Jun 01 '16 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: http://stackoverflow.com/questions/15865829/add-items-to-columns-in-a-wpf-listview http://stackoverflow.com/questions/12828704/listview-win8-xaml-how-to-create-columns – zdunek25 Jun 01 '16 at 15:01
  • I made it, the answer is ExtendedObjects – zdunek25 Jun 04 '16 at 01:02
  • If you have an answer to own question, consider to [post it](http://stackoverflow.com/help/self-answer) to help other users having similar problem. – Sinatr Jun 06 '16 at 07:02

0 Answers0