Thread: WPF ListViewItem subitems

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445

    WPF ListViewItem subitems

    I have a WPF ListView that is not data bound, and it makes no sense to do data binding in my situation. Is there a way to get the data from each column, which, in my case, include two check boxes, a text box, and a combo box, without resorting to data binding? I have searched for information on this extensively now, and have found nothing. In fact, it seems like Microsoft has conveniently omitted this functionality from WPF altogether. The items are displayed, so there must be some way to access them. I just can't find any documentation of how to do it.

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    It seems odd to do this without data binding. After all, the checkboxes, text box, and combo box must all be related to some sort of data object; otherwise you wouldn't need them. Also, ListViews don't really have columns. They're just a collection of ListViewItems.

    If you really don't want to do data binding, then you'll have to traverse the visual tree to get the state of your UI objects. For example, here's a sample ListView:
    Code:
    <ListView Name="lstView">
        <ListViewItem>
            <StackPanel Orientation="Horizontal">
                <CheckBox />
                <CheckBox />
                <TextBox />
                <ComboBox />
            </StackPanel>
        </ListViewItem>
        <ListViewItem>
            <StackPanel Orientation="Horizontal">
                <CheckBox />
                <CheckBox />
                <TextBox />
                <ComboBox />
            </StackPanel>
        </ListViewItem>
        <ListViewItem>
            <StackPanel Orientation="Horizontal">
                <CheckBox />
                <CheckBox />
                <TextBox />
                <ComboBox />
            </StackPanel>
        </ListViewItem>
    </ListView>
    To toggle the second CheckBox on the first ListViewItem, you'd do something like this:
    Code:
    ListViewItem lstViewItem = (ListViewItem)lstView.Items[0];
    Panel p = (Panel)lstViewItem.Content;
    ((CheckBox)p.Children[1]).IsChecked = !((CheckBox)p.Children[1]).IsChecked;
    It's not pretty, but you can do it. Whether or not it's a good idea is a totally different question.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    thanks for the response. I was hoping it wouldn't be this complicated. it just seemed like a lot of extra trouble to set up the data bindings for the check boxes and the combo box, when all I really need to do is read the values out of them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 01-19-2010, 12:16 AM
  2. [C] Editing SubItems in ListView with Win32 API
    By pc2-brazil in forum Windows Programming
    Replies: 9
    Last Post: 12-22-2009, 06:02 PM
  3. Subitems in ListView and
    By P4R4N01D in forum Windows Programming
    Replies: 25
    Last Post: 01-02-2008, 06:14 PM
  4. How do you search subitems of a list view
    By pinkcheese in forum Windows Programming
    Replies: 0
    Last Post: 07-12-2002, 08:46 PM