How to Implement a ListBox of Checkboxes in WPF?

后端 未结 5 1283
南笙
南笙 2021-02-05 06:23

Although somewhat experienced with writing Winforms applications, the... \"vagueness\" of WPF still eludes me in terms of best practices and design patterns.

Despite pop

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-05 07:17

    First you dont need a HeirarchicalDataTemplate for this. Just regular DataTemplate as Aaron has given is enough. Then you need to instantiate the TopicList ObservableCollection somewhere inside the constructor of the class. which makes the ObservableCollection alive even before you add data in to it And binding system knows the collection. Then when you add each and every Topic/CheckedListItem it will automatically shows up in the UI.

    TopicList = new ObservableCollection(); //This should happen only once
    
    private void InitializeTopicList( MyDataContext context )
    {
        TopicList.Clear();
    
        foreach ( Topic topic in topicList )
        {
            CheckedListItem item = new CheckedListItem();
            item.Name = topic.DisplayName;
            item.ID = topic.ID;
            TopicList.Add( item );
        }
    }
    

提交回复
热议问题