0

For some reason, when I write the following code in my Visual Studio 2010 (.NET Framework 4.5):

ListViewItem item = new ListViewItem("item1");
item.SubItems

there is no such field as SubItems.

What could be the reason for that?

Regards, Vitalii.

Larry
  • 17,605
  • 9
  • 77
  • 106
witua
  • 37
  • 1
  • 6

2 Answers2

1

Yes there is, at least in winforms.

Look a little closer, once you add the dot the usual list of items should come up:

enter image description here

ListViewItem item = new ListViewItem("item1");
item.SubItems.Add("subitem1");

Note: To declare an instance you need to reference the class like this:

ListViewItem.ListViewSubItem lvsi = new ListViewItem.ListViewSubItem();

Also note: Sometimes Intellisense stops working; I have to restart VS to bring it back to life..

If however you are targeting WPF, then you are right: There are no SubItems in a WPF ListViewItem. It is a ContentControl, which can contain whatever you put there, I believe. But I'm no WPF expert. A good book may be best..

          System.Windows.Controls.ContentControl
            System.Windows.Controls.ListBoxItem
              System.Windows.Controls.ListViewItem
TaW
  • 53,122
  • 8
  • 69
  • 111
  • Actually when I compile your code I get 'no constructor that takes 1 argument' and 'no Subitems'. – witua Nov 02 '14 at 22:01
  • `Subitems` is wrong It is `SubItems`. Always watch out for the spelling! The code as posted compiles fine. – TaW Nov 02 '14 at 22:14
0

There is a ListViewItem constructor that takes a string array, where the first element is the main item text and the other are the SubItems texts:

var myNewItem = new ListViewItem(new[]
                { 
                    "First column", 
                    "Second column", 
                    "Third column"
                });

myListItem.Items.Add(myNewItem);

Of course, you have to set your ListView Mode to Detail mode, and create the Columns collection accordingly.

EDIT: This is a WinForm tip... I dont know if it applies to WPF unfortunately.

Larry
  • 17,605
  • 9
  • 77
  • 106
  • To make it work, yes. To compile you can leave it in the default View mode. You just see no subitems when you run it.. – TaW Nov 02 '14 at 22:17
  • Well I just realize the question is about intellisense that does not shows "SubItems" property, rather than invisible subitems during runtime. – Larry Nov 02 '14 at 22:18
  • Or maybe he is really targeting WPF..? – TaW Nov 02 '14 at 22:23
  • Unfortunately, both codes doesn't work in my Vusial Studio, and the problem seem to be not about Intellisense (nor spelling...). Your code gives me this: 'System.Windows.Controls.ListViewItem' does not contain a constructor that takes 1 arguments. Maybe that is something wrong with my Visual Studio? I tried on my friend's one and it works fine. – witua Nov 02 '14 at 22:23
  • Yes, I am using WPF. Why? – witua Nov 02 '14 at 22:24
  • OK, not it's seems googleable. I found this one: http://stackoverflow.com/questions/15865829/add-items-to-columns-in-a-wpf-listview. Is it the right way? – witua Nov 02 '14 at 22:27
  • 2
    _Yes, I am using WPF. Why?_ Because there is a completely different control with the same name in WinForms!! Please __always__ tag your question with WPF or ASP or Winforms, where applicable ot you get wrong answers and waste out time.. BTW: The same is true for many other Controls! – TaW Nov 02 '14 at 22:29