Question

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!

Was it helpful?

Solution

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

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

Thanks

OTHER TIPS

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。

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top