how to add user controls as listbox items

无人久伴 提交于 2019-12-06 02:19:45

问题


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!


回答1:


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

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

Thanks




回答2:


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。



来源:https://stackoverflow.com/questions/18552716/how-to-add-user-controls-as-listbox-items

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!