Skip to main content

Microsoft Silverlight

Answered Question How to select All lst item from Silverlight 2 beta 2 listboxRSS Feed

(0)

sajesh28l@gmail.com
sajesh28...

Member

Member

1 points

26 Posts

How to select All lst item from Silverlight 2 beta 2 listbox

Hi, I have one listbox i want to select all the list item from this listbox.How i can use foreach statement in list box control.can anybody help me to this? how i can hnadle foreach for this list box.this box is dynamically filled at run time thx in advance regards, Sajesh

Jim Mangaly
Jim Mangaly

Contributor

Contributor

2610 points

380 Posts

Re: How to select All lst item from Silverlight 2 beta 2 listbox

Try using the VisualTreeHelper.GetChild() method in a loop:  http://msdn.microsoft.com/en-us/library/system.windows.media.visualtreehelper.getchild(VS.95).aspx

(If this answers your question, MARK this reply as the answer)

Jim (http://jimmangaly.blogspot.com/)

http://www.identitymine.com/

rajesh shirpuram
rajesh s...

Contributor

Contributor

2314 points

505 Posts

Re: How to select All lst item from Silverlight 2 beta 2 listbox

 hi sajesh,

Chk this link out 

Thanks
Rajesh Shirpuram

(If this has answered your question, please click on "mark as answer" on this post. Thank you!)

sajesh28l@gmail.com
sajesh28...

Member

Member

1 points

26 Posts

Re: How to select All lst item from Silverlight 2 beta 2 listbox

Hi jim, Thanks for spending time for reply to my post.Can you give me sample codes for how i can use VisualTreeHelper.GetChild() .My requirement is filling and show all list items as selected in listbox on runtime. like listbox.itemsIdea.selected =true; Regards, Sajesh

Jim Mangaly
Jim Mangaly

Contributor

Contributor

2610 points

380 Posts

Re: How to select All lst item from Silverlight 2 beta 2 listbox

Hi Sajesh, you can use the following helper function that uses the VisualTreeHelper.GetChild() method:

private void GetChildren(UIElement parent, Type targetType, ref List<UIElement> children)
        {
            int count = VisualTreeHelper.GetChildrenCount(parent);
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    UIElement child = (UIElement)VisualTreeHelper.GetChild(parent, i);
                    if (child.GetType() == targetType)
                    {
                        children.Add(child);
                    }
                    GetChildren(child, targetType, ref children);
                }
            }
        }

Then call this method like this:

List<UIElement> result = new List<UIElement>();
GetChildren(nameofYourListBox, typeof(ListBoxItem), ref result);

"result" will have the result :). Now you have all the ListBoxItems in a List.

(If this answers your question, MARK this reply as the answer)

Jim (http://jimmangaly.blogspot.com/)

http://www.identitymine.com/

sajesh28l@gmail.com
sajesh28...

Member

Member

1 points

26 Posts

Re: How to select All lst item from Silverlight 2 beta 2 listbox

Hi jim, Thanks very much for spent time to help me.But my requirement is not like that In my application I was using onle list box with Check boxes as item templte.,so user can select multiple check boxes's .After user selecting check boxes i want to pass all checkboxes in listbox what is checked  to another list box.i mean i want to dynamically add the  checkboxes[what is checked] in another list box  . can't iterate through the list of checkboxes in a Listbox  to see what is checked. i use DataTemplate to display content.how to find checkbox controls in listbox?

My Xaml code,

 

  <ListBox Height="150" x:Name="lstfeilds" VerticalAlignment="Center" Width="160" Canvas.Left="90" ScrollViewer.VerticalScrollBarVisibility="Visible" DisplayMemberPath="Tname" DataContext="Tname" >
                               <ListBox.ItemTemplate>
                                   <DataTemplate>
                                    <CheckBox Content="{Binding Tname}" Margin="2" IsChecked="false" ClickMode="Press"                   
Click="CheckBox_Click"/>                                
                                    </DataTemplate>
                                 </ListBox.ItemTemplate>
                             </ListBox>

 

  can youe help me?

Thanks in advance,

 

Regards, Sajesh

Jonathan Shen – MSFT
Jonathan...

All-Star

All-Star

23562 points

2,304 Posts

Microsoft
Answered Question

Re: How to select All lst item from Silverlight 2 beta 2 listbox

Hi Sajesh,

Below is a working sample. Please compare it with yours. 

<UserControl
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	x:Class="SLTest1.Page"
	Width="640" Height="480">

	<Grid x:Name="LayoutRoot" Background="White" >
		<ListBox x:Name="myListBox" Margin="27,80,0,238" VerticalAlignment="Stretch" HorizontalAlignment="Left" Width="222">
            <ListBox.ItemTemplate>
                <DataTemplate>
                        <CheckBox Content="{Binding}" IsChecked="false" ClickMode="Press" Click="CheckBox_Click"></CheckBox>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <ListBox x:Name="destListBox" HorizontalAlignment="Right" Margin="0,80,54,238" VerticalAlignment="Stretch" Width="222">
             
        </ListBox>
    </Grid>
</UserControl>
 
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace SLTest1
{
	public partial class Page : UserControl
	{
        List<string> myList = new List<string>();
        public Page()
		{
			// Required to initialize variables
			InitializeComponent();
            this.Loaded += new RoutedEventHandler(Page_Loaded);
		}

        void Page_Loaded(object sender, RoutedEventArgs e)
        {
            LoadData(); 
        }

       
        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            CheckBox curCheckBox = sender as CheckBox;
            if (curCheckBox.IsChecked == true)
            {
                //delete the checked item.
                //myList.Remove(curCheckBox.Content.ToString());
                //myListBox.ItemsSource = null;
                //myListBox.ItemsSource = myList;

                ListBoxItem newItem = new ListBoxItem();

                newItem.Content = curCheckBox.Content;
                destListBox.Items.Add(newItem);
            }
            else 
            {
                List delList = new List();
                foreach (ListBoxItem lbi in destListBox.Items) 
                {
                    if (lbi.Content.ToString() == curCheckBox.Content.ToString()) 
                    {
                        delList.Add(lbi);
                    }
                }
                foreach (ListBoxItem lbi in delList) 
                {
                    destListBox.Items.Remove(lbi);
                }
            }
        }
        private void LoadData() 
        {
            
            myList.Add("aaaaa");
            myList.Add("bbbbb");
            myList.Add("ccccc");
            myList.Add("ddddd");
            myList.Add("eeeee");
            myList.Add("fffff");
            myListBox.ItemsSource = myList;
        }
        
	}
}

Best regards,

Jonathan  

 

Jonathan Shen
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities