4

How do you add sub-items to a ListView? I need to generate everything dynamically, but every example I've found uses XAML.

Non-WPF was so simple:

ListViewItem lvi = listview.items.add(wahtever);
lvi. blah blah blah

How do you add sub-items in WPF without using XAML?

Grant Winney
  • 65,241
  • 13
  • 115
  • 165
nitefrog
  • 1,760
  • 6
  • 31
  • 59
  • 1
    The WPF listview dont have 'subitems' like the old winforms does. It requires a very different mindset. I thought exactly the way you are now, What the hell is with all the XAML? Winforms so much more straight forward. But now, I cant go back to winforms. WPF wins in my book hands down. Just takes a bit of time to get your head around it but when you do. It's awesome. – John Petrak Jan 14 '11 at 02:54
  • 1
    Yep I remember my first few months with WPF ... my baldness increased... Now any UI platform I think about, I want it to have Bindings, Themes, Styles, Triggers, DataContexts, Commands, Attached Properties. Damn WinForms are so lame! :p – WPF-it Sep 09 '11 at 07:59

3 Answers3

8

As already mentioned, WPF doesn't have sub-items like WinForms. Instead you use properties on an object that suits your purposes.

For completeness, here is XAML contrasted with code.

XAML:

    <UniformGrid Columns="2">
        <ListView Name="xamlListView">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="X Value" DisplayMemberBinding="{Binding X}"/>
                    <GridViewColumn Header="Y Value" DisplayMemberBinding="{Binding Y}"/>
                </GridView>
            </ListView.View>
            <ListView.Items>
                <PointCollection>
                    <Point X="10" Y="20"/>
                    <Point X="20" Y="30"/>
                </PointCollection>
            </ListView.Items>
        </ListView>
        <ListView Name="codeListView"/>
    </UniformGrid>

Code:

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        var view = new GridView();
        view.Columns.Add(new GridViewColumn { Header = "First Name", DisplayMemberBinding = new Binding("First") });
        view.Columns.Add(new GridViewColumn { Header = "Last Name", DisplayMemberBinding = new Binding("Last") });
        codeListView.View = view;
        codeListView.Items.Add(new { First = "Bill", Last = "Smith" });
        codeListView.Items.Add(new { First = "Jane", Last = "Doe" });
    }
Rick Sladkey
  • 33,988
  • 6
  • 71
  • 95
  • Thank you so much. That did the trick. So now it seems that with everything you use the binding even through code? Interesting and frustrating change, but I thank you so much for your help as it is greatly appreciated! – nitefrog Jan 14 '11 at 03:35
6

The "WPF way" would be to bind your listview to a collection that represents the data you want to display. Then add objects containing data to that collection. You almost never should have to deal with adding ListViewItems to your list manually as you are planning to do. I could come up with an example but there's many threads here on SO already that solve exactly this problem:

  1. Add programmatically ListViewItem to Listview in WPF
  2. WPF ListView - how to add items programmatically?
Community
  • 1
  • 1
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
  • I need one favor, How can i change the background color by add rows dynamically as above? – Sandy Jan 28 '11 at 05:13
-3

SubItem or ListItem?? Easy:

myListbox.Items.Add(object);

You can add any type of object into that collection or a specific ListBoxItem object like this one:

this.List.Items.Add(new ListBoxItem{ Content = "Value to display"});
Raffaeu
  • 6,694
  • 13
  • 68
  • 110