Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

How to select All lst item from Silverlight 2 beta 2 li... RSS

6 replies

Last post Aug 12, 2008 08:57 AM by Jonathan Shen – MSFT

(0)
  • sajesh28l@gmail.com

    sajesh28l@gm...

    Member

    1 Points

    26 Posts

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

    Aug 04, 2008 04:36 AM | LINK

    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

    ListBox Template

  • Jim Mangaly

    Jim Mangaly

    Contributor

    2646 Points

    383 Posts

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

    Aug 04, 2008 04:50 AM | LINK

    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 shirp...

    Contributor

    2315 Points

    507 Posts

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

    Aug 04, 2008 04:56 AM | LINK

     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

    sajesh28l@gm...

    Member

    1 Points

    26 Posts

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

    Aug 04, 2008 06:49 AM | LINK

    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.items[i].selected =true; Regards, Sajesh
  • Jim Mangaly

    Jim Mangaly

    Contributor

    2646 Points

    383 Posts

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

    Aug 04, 2008 08:36 AM | LINK

    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

    sajesh28l@gm...

    Member

    1 Points

    26 Posts

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

    Aug 07, 2008 03:05 AM | LINK

    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

    ListBox Template

  • Jonathan Shen – MSFT

    Jonathan She...

    All-Star

    50156 Points

    4951 Posts

    Microsoft

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

    Aug 12, 2008 08:57 AM | LINK

    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  

     

    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com.
    Microsoft One Code Framework