Visual Studio 2010 WPF and ListBoxes
-
Good day, I wan't to ask something that is obvious to do in VS 2005 and VS 2008 (WPF), but for some reason, I am struggling my but of in VS 2010 (WPF). :wtf: I want to dynamically load items into a ListBox, but for some reason it does not want to show up in the ListBox, they are there (I perform a count on the ListBox, and it shows the expected number). Here is how I do it: First of, I add an item like this: this.lstItems.Items.Add(OtherStackPanel(temp));, where OtherStackPanel is a function that returns a populated stackpanel, with the following items: 1 x Label, 1 x TextBox, 1 x ComboBox, 3 x Seperator, 1 x Slider, which look as follows
__________________________________________________________________________
| | | | | | | |
|ComboBox | Seperator | Label | Seperator | TextBox | Seperator | Slider |
|__________|___________|_______|___________|_________|___________|________|This would then be added to the ListBox, all the items in the StackPanel are set to be visible, and they have a parent since they are added to the StackPanel. After I have added the StackPanel, I call the LayoutUpdated event of the ListBox, to make sure that they are contained in the ListBox, but I also add two addisional items from within this eventhandler, as follows:
private void listBox1_LayoutUpdated(object sender, EventArgs e)
{
if (sender != null)
{
ListBoxItem l = new ListBoxItem();
l.Content = "S";
ListBoxItem n = new ListBoxItem();
n.Content = "A";
((ListBox)sender).Items.Add(l);
this.lstItems.Items.Add(n);
MessageBox.Show(((ListBox)sender).Name);
MessageBox.Show(Convert.ToString(((ListBox)sender).Items.Count));
}
}((ListBox)sender).Name returns ListBox, which is correct, since the item passed to this event is that listbox, and ((ListBox)sender).Items.Count returns 3, which is correct, since I have added the StackPanel, "S" and "A", but for some odd reason, I cannot see the items in the ListBox. I have tried this.lstList.Items.Insert(0, "S"), but to no avail! If you could provide a possible solution, I would really appreciate it (I don't know why it is so much different than VS 2008 WPF). :confused: I am using Windows 7 Ulitmate. Kind Regards, Rossouw :)