Programmatically binding List to ListBox

后端 未结 4 576
说谎
说谎 2020-12-09 09:25

Let\'s say, for instance, I have the following extremely simple window:



        
4条回答
  •  不知归路
    2020-12-09 09:30

    First you'd need to give your ListBox a name so that it's accessible from your code behind (edit I note you've already done this, so I'll change my example ListBox's name to reflect yours):

    
    

    Then it's as simple as setting the ListBox's ItemsSource property to your list:

    eventList.ItemsSource = ListOfNames;
    

    Since you've defined your "ListOfNames" object as a List, the ListBox won't automatically reflect changes made to the list. To get WPF's databinding to react to changes within the list, define it as an ObservableCollection instead.

提交回复
热议问题