0

I have a list view which has a grid view with four columns. The itemsSource for the listview is an IList(Of SomeType). Each cell in the grid contains a checkboxes, which are checked/unchecked based on the values in the bound property. Now i want to retrieve all the rows in list/grid view for saving purposes or atleast all those checkboxes that are checked. I couldn't find a suitable way to do that.

Here is how i create my listview.

                <ListView Margin="10, 40, 95, 10" x:Name="ListViewPane">
                    <ListView.View>
                        <GridView x:Name="gridColumns">
                            <GridViewColumn Width="auto" Header="Right" DisplayMemberBinding="{Binding Name}"/>
                            <GridViewColumn Width="auto" Header="Read">
                                <GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                        <CheckBox Margin="0" VerticalAlignment="Center" IsChecked="{Binding CanRead}"/>
                                    </DataTemplate>
                                </GridViewColumn.CellTemplate>
                            </GridViewColumn>
                            <GridViewColumn Width="auto" Header="Write">
                                <GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                        <CheckBox Margin="0" VerticalAlignment="Center" IsChecked="{Binding CanWrite}"/>
                                    </DataTemplate>
                                </GridViewColumn.CellTemplate>
                            </GridViewColumn>
                            <GridViewColumn Width="auto" Header="Delete">
                                <GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                        <CheckBox Margin="0" VerticalAlignment="Center" IsChecked="{Binding CanDelete}"/>
                                    </DataTemplate>
                                </GridViewColumn.CellTemplate>
                            </GridViewColumn>
                        </GridView>
                        </ListView.View>
                </ListView>

Can anyone help me????

2 Answers 2

3

You can iterate through the Items object of the ListView to get the values:

foreach( var item in this.ListViewPane.Items )
{
    var ofSomeType = item as OfSomeType;
    if( ofSomeType != null )
    {
        string name = ofSomeType.Name;
        bool canDelete = ofSomeType.CanDelete;
        bool canRead = ofSomeType.CanRead;
        bool canWrite = ofSomeType.CanWrite;

        // do stuff with your Of Some Type objects
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

When it comes to WPF, be sure to use "var" instead of ListViewItem in the foreach statement, else you will get a compilation error. ListView.Item is not necessarily the same as a ListViewItem, might confuse some.
1

You need to set the IsChecked Bindings to Mode=TwoWay, e.g.

IsChecked="{Binding CanRead, Mode=TwoWay}"

Then WPF will update your business objects as the user checks and unchecks the boxes.

Now you can just collect the values directly from your business object collection (the ItemsSource):

For Each busobj In ListViewPane.ItemsSource
  If busobj.CanDelete Then
    ' whatever
  End If
Next

(pardon any syntax errors in the VB)

If you really need to access the ListViewItem controls that represent the physical rows in the UI control, you can get at them using the ItemContainerGenerator:

For Each busobj In ListViewPane.ItemsSource
  Dim lvi As ListViewItem = CType(ListViewPane.ItemContainerGenerator.ContainerFromItem(busobj), ListViewItem)
Next

Comments

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.