tags:

views:

274

answers:

2

Hi, 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????

+1  A: 

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
itowlson
thanks a lot itowlson
Xience
A: 

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
    }
}
Metro Smurf