Getting ListBox Items Silverlight - silverlight

I have tried to get the listbox items using VisualTreeHelper class. When I do VisualTreeHelper.GetChildrenCount((DependencyProperty)listBox1) it returns count as 0. But the listbox has lot of listboxitems in it.Can someone let me know if I am doing any mistake?
Regards,
Lalith

Have you tried to use the ItemContainerGenerator property of the Listbox class ? It has some methods you can use to retrieve Listbox items.
http://msdn.microsoft.com/en-US/library/system.windows.controls.itemscontrol.itemcontainergenerator(v=VS.95).aspx

Related

Does TreeView create a new ItemContainerGenerator per Node/Level?

I am wondering if there is one "global" generator for TreeView or does a new ItemContainerGenerator gets created on each node/level, when expanded?
Anyone who had to deal with this depth of wpf?
Thanks again in advance I appreciate your help much. :) :)
I posted this question to msdn forum as well. And this is what they answered to me.
I guess I was right. Each node has its own generator.
Check it out:
http://social.msdn.microsoft.com/Forums/en-US/d36d164a-296e-44d5-80a6-4f09414d0a64/does-treeview-create-a-new-itemcontainergenerator-per-nodelevel?forum=wpf
ItemContainerGenerator for any ItemsControl (including TreeView) is associated with the control and is responsible for generating the UIElement items on behalf of a ItemsControl.
ItemContainerGenerator generates each and every item, not each item creates its own ItemContainerGenerator.
But as rightly pointed out by you, treeviewitem is in itself itemscontrol so each will have its generator which will generate its child.
MSDN ref: http://msdn.microsoft.com/en-us/library/system.windows.controls.itemcontainergenerator.aspx
The ItemsControl generates its items through the
IItemContainerGenerator interface. The ItemContainerGenerator property
of the ItemsControl is of type ItemContainerGenerator, which
implements the IItemContainerGenerator interface.

WPF Accessing Items inside Listbox which has a UserControl as ItemTemplate

I have Window that has a ListBox
ListBox(MyListBox) has a DataTable for its DataContext
ListBox's ItemSource is : {Binding}
Listbox has a UserControl(MyUserControl) as DataTemplate
UserControl has RadioButtons and TextBoxes (At first They're filled with values from DataTable and then user can change them)
Window has one Submit Button
What I want to do is, when user clicks the submit button
For each ListBox Item, get the values form UserControl's TextBoxes and RadioButtons.
I was using that method for this job :
foreach(var element in MyListBox.Items)
{
var border = MyListBox.ItemContainerGenerator.ContainerFromItem(element)as FrameworkElement;
MyUserControl currentControl = VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(myBorder,0) as Border,0)as ContentPresenter,0)as MyUserControl;
//And use currentControl
}
I realised nothing when using 3-5 items in Listbox. But when I used much more items, I saw that "var border" gets "null" after some elements looped in foreach function.
I found the reason here :
ListView.ItemContainerGenerator.ContainerFromItem(item) return null after 20 items
So what can I do now? I want to access all items and get their values sitting on user controls.
Thanks
You should use objects who implement INotifyPropertyChanged and bind an ObservableCollection of it to the ItemSource
And then you can get all the list of items.
Here some quick links from MSDN to get more informations
How to: Implement Property Change Notification
Binding Sources Overview
You should google for some tutorials about this.
Zied's post is a solution for this problem. But I did the following for my project:
I realised that there's no need to use UserControl as DataTemplate in my project. So I removed ListBox's DataTemplate.
I removed MyListBox.DataContext = myDataTable and used this:
foreach(DataRow dr in myDataTable.Rows)
{
MyUserControl muc = new MyUserControl(dr);
myListBox.Items.Add(muc);
}
I took DataRow in my UserControl's constructor and did what I want.
And at last I could access my UserControls in ListBox by using :
foreach(MyUserControl muc in
myListBox)
{
//do what you want
}
Easy huh? :)

PieSeries in a listboxItem

I have a listboxitem whose datatemplate contains a pieseries. The listbox is bound to the class having a dictionary member. I want to bind that dictionary member to the pieseries. I have tried couple of ways, but it did not work. it is generating blank pieseries. The pieseries datacontext is being set to the datacontext of the listboxitem and still it is not working.
let me know if you have any suggestions.
Got it working after long struggle just by writing a converter to do the calculation which were there in the dictionary.

autoupdate a list in wpf

I want to display a bunch of Objects i have created in a ListBox. My objects implement the INotifyPropertyChanged Interface.
I tried to use an ObservableCollection, which i have bound to a listbox Control (listbox1.DataContext = MyCollection)
But this does not exactly what i want to do, because the Listbox is not refreshed when one of the properties of one of my objects in MyCollection changes.
I have found this blogposting: http://sweux.com/blogs/psampaio/index.php/2009/04/13/creating-a-custom-observable-collection-in-wpf
is this realy the easyiest/only way to keep track of several objects?
I'm not sure, but have you tried using a datatemplate for your listbox items? like, a textbox that explicitly sets it's text to the appropriate binding.

How to bind a custom value to the Text property of a TextBlock inside of a ListBox DataTemplate?

I would like my ListBox to number each ListItem using its index + 1.
How would I do that to the Text property of a TextBlock in a DataTemplate of the ListBox?
If each ListBoxItem uses SelectedIndex + 1, they will all display the same value since SelectedIndex is a scalar. Moreover, this number will change as the user selects different ListBoxItems. I suspect you actually want to display each item's index within the ListBox + 1.
To achieve this, you're probably best off using the ListBox's ItemsContainerGenerator to get the index of the item within the container (see the IndexFromContainer method). You could look at exposing this from your data class, or perhaps look into an attached readonly property that retrieves this value for you.
I had the same question. So far I'm just using my data model to provide the numbers...

Resources