Skip to main content
Home Forums Silverlight Programming Programming with .NET - General How to select All lst item from Silverlight 2 beta 2 listbox
6 replies. Latest Post by Jonathan Shen – MSFT on August 12, 2008.
(0)
sajesh28...
Member
1 points
26 Posts
08-04-2008 12:36 AM |
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
Contributor
2622 points
381 Posts
08-04-2008 12:50 AM |
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/)
rajesh s...
2314 points
505 Posts
08-04-2008 12:56 AM |
hi sajesh,
Chk this link out
08-04-2008 2:49 AM |
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.selected =true; Regards, Sajesh
08-04-2008 4:36 AM |
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.
08-06-2008 11:05 PM |
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...
All-Star
24939 points
2,425 Posts
08-12-2008 4:57 AM |
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