Search code examples
c#listviewcheckboxwindows-store-appslistviewitem

get checked items of listview using checkbox windows store app c#


I am developing one Windows store application. I have implemented one listview. listview contains image , textblock and checkbox controls. my listview gets the data from internet i have done xml parsing with listview and binded data to listview. i want to get all the data from listview where checkboxes are checked in listview. my xaml code is:

<ListView Name="display" ItemsSource="{Binding}"   SelectionMode="Single" 
 SelectionChanged="display_SelectionChanged" 
 ScrollViewer.HorizontalScrollMode="Enabled" ScrollViewer.HorizontalScrollBarVisibility="Visible"   
 ItemContainerStyle="{StaticResource ListViewItemStyle12}" >
   <ListView.ItemsPanel>
      <ItemsPanelTemplate>
         <StackPanel x:Name="stak2" Orientation="Horizontal" />
      </ItemsPanelTemplate>
   </ListView.ItemsPanel>
    <ListView.ItemTemplate>
       <DataTemplate>
          <StackPanel Orientation="Vertical">
            <Image Source="{Binding  Path=Image}" Width="450" Tapped="image_taped" />
            <CheckBox Tag="{Binding Path=tag}" Visibility="{Binding Path=visichk}" Height="40"      Name="addremove" 
                HorizontalAlignment="Center" Checked="add_checked" Unchecked="sub_checked"  Opacity="0.5" 
                 Background="White" VerticalAlignment="Top" Template="{StaticResource CheckboxImageTemplate}" >
            </CheckBox>
            <TextBlock Text="{Binding Image_code}" FontSize="25" Foreground="Gray" HorizontalAlignment="Center"  />
          </StackPanel>
       </DataTemplate>
     </ListView.ItemTemplate>
 </ListView>

datasource for listview :

XDocument xmlDoc = XDocument.Parse(responseString);
var Categories = xmlDoc.Descendants("product").ToArray();
List<ProductData> displaylst = new List<ProductData>(); //ProductData is my Class.

foreach (var cat in Categories)
{
    string prId = cat.Elements("id_products").Select(r => r.Value).FirstOrDefault();   
    List<string> Image = cat.Descendants("images").Elements("src").Attributes("largimage").Select(r => r.Value).ToList();

    List<string> Image_code = cat.Descendants("images").Elements("src").Select(r => r.LastAttribute.Value).ToList();
    int i = 0;
    foreach (string img in Image)
    {
        displaylst.Add(new ProductData { Id = prId, Image = img, Image_code = Image_code[i] });
        i++;
    }
}

display.ItemsSource = displaylst;            

Now on one button click i want to get the data of Product like prId,Image,Image_code where checkbox are checked from listview and put it into the simple list.

how can i did this please help me. thanks in advance.


Solution

  • First let's add a property to your ProductData class

    public class ProductData
    {
        public string Id { get; set; }
        public string Image { get; set; }
        // I dont know exactly what's in this class
        // ... more properties
    
        // Add this one
        public bool IsSelected { get; set; }  
    }
    

    Now that we have a boolean IsSelected in our ProductData class we can know which are selected.

    In the second foreach change this line

    // Set IsSelected to false by default
    displaylst.Add(new ProductData { IsSelected = false, Id = prId, Image = img, Image_code = Image_code[i] });
    

    And bind the "IsChecked" property of your checkbox to IsSelected

    <CheckBox IsChecked="{Binding Path=IsSelected}" Tag="{Binding Path=tag}" Visibility="{Binding Path=visichk}" Height="40"      Name="addremove" 
                HorizontalAlignment="Center" Checked="add_checked" Unchecked="sub_checked"  Opacity="0.5" 
                 Background="White" VerticalAlignment="Top" Template="{StaticResource CheckboxImageTemplate}" >
    

    With binding when you check one of the checkbox, the associed productData IsSelected property will become "true" automatically.

    So now you just have to do a new list and select only ProductData where IsSelected is true:

    List<ProductData> listOfSelectedProducts = (from product in displaylst 
                                                where product.IsSelected == true
                                                select product).ToList();
    

    Here you go you got a list of ProductData with only selected products.