Programmatically Add Controls to WPF Form

前端 未结 3 1206
悲哀的现实
悲哀的现实 2021-01-31 04:02

I am trying to add controls to a UserControl dynamically (programatically). I get a generic List of objects from my Business Layer (retrieved from the database), and for each o

3条回答
  •  鱼传尺愫
    2021-01-31 04:32

    It is not recommended to add controls like this. What you ideally do in WPF is to put a ListBox(or ItemsControl) and bind your Business object collection as the itemsControl.ItemsSource property. Now define DataTemplate in XAML for your DataObject type and you are good to go, That is the magic of WPF.

    People come from a winforms background tend to do the way you described and which is not the right way in WPF.

提交回复
热议问题