5

I am using WPF .net 4.5 (c#) and I would like to make a ListBox that contains a series of user controls. (If a listbox is the wrong type of control, please let me know).

I want my listbox to have a copy of the user control as the list items, with different contents within each one.

How do I add user controls to a listbox?

Thanks for your help in advance!

H.B.
  • 166,899
  • 29
  • 327
  • 400
Rob
  • 6,819
  • 17
  • 71
  • 131

2 Answers2

7

You can set ItemTemplate for the listbox with your usercontrol in it.

    <ListBox>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <local:UserControl1/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

Thanks

Nitin
  • 18,344
  • 2
  • 36
  • 53
0

I think you can design a DataTemplate which ha the same UI or Style as this UserControl . maybe just copy paste can get a DataTemplate as you want,however

 <DataTemplate>
  <local:UserControl1/>
  </DataTemplate>

this king seems very strange for me I dont konw it can work as you want,so I also want to know the answers。

Kobe
  • 19
  • 1