Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone!

I have searched for the past couple of days trying to find the answer to this. :(

C#
List<SomeClass> myLists = new List<SomeClass>();

class SomeClass
{
    public string Name{get;set;}
    public List<string> StringList {get;set;}
}


So, using the previous code as an example, is there a way in wpf to use myLists to dynamically create ListBoxes in something like a StackPanel with a horizonal orientation and then bind the corresponding StringList to the ItemsSource property to populate the items in the ListBox?

The main problem I'm having is just finding a way to dynamically create the ListBoxes. I've been working with datatemplates to try to get this to work, but I'm new to datatemplates and I'm probably missing something.

Thank you in advance for the help!
Posted

You can add anything you want in the code.

I usually advise a very general method of solving such problems. If you don't know how to put some control, create some layout, etc., try to do it using the designer first. Test the application to see the results. If this is what you want, locate the auto-generated code and learn how it works.

With WPF, it's a bit trickier to find those auto-generated files, because they are not shown in your Solution Explorer tree view but are created as temporary file. You can use some file search tool (not the one of VS) to find them. Normally, they are put in "obj\Debug" or "obj\Relese" sub-directories relative to your project (more exactly, "obj\$(configuration_name)").

In the simplest case, adding a control looks like this:
C#
ListBox lb = new ListBox();
this.parentPanel.Children.Add(lb);

Above, I assumed that you have some panel of the type derived from System.Windows.Controls.Panel you want to use as a parent for other control. See MSDN help on each control to find out what properties should you use to set up desired layout and other features. Again, looking at the auto-generated code is good to see what's involved.

You will need to use exact same techniques you find in your auto-generated code. Only don't use ugly naming of the variables and method designers usually use — such names violate (good) Microsoft naming conventions. Give everything semantically sensible names, without numeric characters and other "automated" dirt.

Good luck,
—SA
 
Share this answer
 
v2
You can always have a ListBox with a ListBox in the DataTemplate (see to see how to create a DataTemplate http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/760eab7e-63c0-45ac-b7fc-167e754354ce[^]). You can put the Items in a different container by using ItemsPanelTemplate:

XML
<ItemsPanelTemplate>
  <StackPanel />
</ItemsPanelTemplate>


Note: to make this work, you have to have an IEnumerable list that contains another IEnuemrable.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900