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
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
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?
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.
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
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
ListBox Template
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/)
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
Rajesh Shirpuram
(If this has answered your question, please click on "mark as answer" on this post. Thank you!)
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
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/)
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 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.
Best regards,
Jonathan
If you have any feedback about my replies, please contact msdnmg@microsoft.com.
Microsoft One Code Framework