sajid raja

sajid raja

  • NA
  • 66
  • 47.3k

How to Check/uncheck all ietms in listbox in Windopws phone

Jul 6 2013 2:33 PM
Hello
I have a listbox in windows phone and i want had bind it with all contacts in my Phone. I want to check/uncheck all items in listbox on button click. My XML code is 
 <ListBox Name="lstcontacts" ItemsSource="{Binding}" Margin="10,93,10,113"  SelectionMode="Multiple" Foreground="#FF333333" >
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">

                            <StackPanel Orientation="Horizontal" Tag="{Binding ObjectID}">
                                <CheckBox x:Name="contactChk" Tag="{Binding}" IsChecked="true" IsEnabled="True" Foreground="Black" Background="Black" BorderBrush="Black" Checked="chbox_checked" />
                                <TextBlock Name="txtResults1" Text="{Binding Path=DisplayName, Mode=TwoWay}"  />
                            </StackPanel>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

            


            
            <Button Content="Check All" HorizontalAlignment="Left" Margin="0,499,0,0" VerticalAlignment="Top" Click="Button_Click_1"/>
            <Button Content="Uncheck All" HorizontalAlignment="Left" Margin="138,499,0,0" VerticalAlignment="Top"/>
            <Button Content="Backup" HorizontalAlignment="Left" Margin="304,499,0,0" VerticalAlignment="Top" Width="142"/>
            

And C# code is 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Phone.Data;
using Microsoft.Phone.UserData;
using Microsoft.Phone.Tasks;
using System.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;


namespace MyPhoneBackup
{
    public partial class CreateContactBackups : PhoneApplicationPage
    {
        //AddressChooserTask addressChooserTask;
        //PhoneNumberChooserTask addressChooserTask;

        

        public CreateContactBackups()
        {
            InitializeComponent();
            
            Contacts cContacts = new Contacts();
            cContacts.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(ContactsSearch);
            cContacts.SearchAsync(String.Empty, FilterKind.DisplayName, null);



            /* addressChooserTask = new PhoneNumberChooserTask();
            addressChooserTask.Completed += new EventHandler<PhoneNumberResult>(addressChooserTask_Completed); 
            //addressChooserTask.Completed += new EventHandler<AddressResult>(addressChooserTask_Completed); */


        }

        /*
         void addressChooserTask_Completed(object sender, PhoneNumberResult e)
         {
             if (e.TaskResult == TaskResult.OK)
             {
                 txtDisplay.Text = "The address for " + e.DisplayName + " is " + e.PhoneNumber;
             }
         }*/


        private void Button_Click_2(object sender, RoutedEventArgs e)
        {


            /*try
            {
                addressChooserTask.Show();
            }
            catch (System.InvalidOperationException ex)
            {
            }*/

        }


        void ContactsSearch(object sender, ContactsSearchEventArgs e)
        {
            try
            {
                lstcontacts.DataContext = e.Results;
            }
            catch (System.Exception)
            {
                txtResults.Text = "No Results Available";
            }
            if (lstcontacts.Items.Any())
            {
                txtResults.Text = "Below is the List of Contacts";
            }
            else
            {
                txtResults.Text = "No Results Available";
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
                       //CheckBox contactChk = lstcontacts.SelectedItem as CheckBox;
           // SearchVisualTree(this.lstcontacts);
           // int count22 = lstcontacts.Children.Count;

            //StackPanel FirstPanel = GetChild<StackPanel>(lstcontacts) as StackPanel;
            //StackPanel SecondPanel = GetChild<StackPanel>(FirstPanel) as StackPanel;
            //int i = lstcontacts.Items.Count;
            //for (int j = 0; j <= i; j++ )
            //{

            //    ListBoxItem item = this.lstcontacts.ItemContainerGenerator.ContainerFromIndex(Convert.ToInt32(j)) as ListBoxItem;
            //    CheckBox contactChk = FindFirstElementInVisualTree<CheckBox>(item);
            //    contactChk.IsChecked = false;
            //}

            StackPanel FirstPanel = GetChild<StackPanel>(lstcontacts) as StackPanel;
            StackPanel SecondPanel = GetChild<StackPanel>(FirstPanel) as StackPanel;

            for (int n = 0; n < SecondPanel.Children.Count; n++)
            {
               // CheckBox CheckBoxItem = GetChild<CheckBox>(SecondPanel);
                ListBoxItem item = this.lstcontacts.ItemContainerGenerator.ContainerFromIndex(Convert.ToInt32(n)) as ListBoxItem;
                CheckBox CheckBoxItem = FindFirstElementInVisualTree<CheckBox>(item);
                CheckBoxItem.IsChecked = false;
            }

        }

        /**/

        public T GetChild<T>(DependencyObject obj) where T : DependencyObject
        {
            DependencyObject child = null;

            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                child = VisualTreeHelper.GetChild(obj, i);

                if (child != null && child.GetType() == typeof(T))
                    break;

                else if (child != null)
                {
                    child = GetChild<T>(child);

                    if (child != null && child.GetType() == typeof(T))
                        break;
                }
            }
            return child as T;
        }

        /**/

        private void SearchVisualTree(DependencyObject targetElement)
        {
            var count = VisualTreeHelper.GetChildrenCount(targetElement);
            if (count == 0)
                return;

            for (int i = 0; i < count; i++)
            {
                var child = VisualTreeHelper.GetChild(targetElement, i);
                if (child is TextBlock)
                {
                    TextBlock myItems = (TextBlock)child;
                    if (myItems.Text == "Item2")                    {
                        myItems.Foreground = new SolidColorBrush(Colors.Green);
                        return;
                    }
                }
                else
                {
                    SearchVisualTree(child);
               }
            }

        }

        private T FindFirstElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject
        {
            var count = VisualTreeHelper.GetChildrenCount(parentElement);

            if (count == 0)
                return null;
            for (int i = 0; i <= count; i++)
            {
                var child = VisualTreeHelper.GetChild(parentElement, i);
                if (child != null && child is T)
                {
                    return (T)child;
                }
                else
                {
                    var result = FindFirstElementInVisualTree<T>(child);
                    if (result != null)
                        return result;
                }
            }
            return null;
        }

        public void chbox_checked(object sender, RoutedEventArgs e) {
            


        }
   
    }

   

}


In this code only first two items in list box are being checked/uncheck on button click. Please help me how can i check/uncheck all items in list on button click