How to Implement a ListBox of Checkboxes in WPF?

后端 未结 5 1282
南笙
南笙 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:21

    Use ObservableCollection instead of List

    Edit

    it implements INotifyCollectionChanged interface to let WPF know when you add/remove/modify items

    Edit 2

    Since you set TopicList in code, it should be a Dependency Property, not a common field

        public ObservableCollection TopicList {
            get { return (ObservableCollection)GetValue(TopicListProperty); }
            set { SetValue(TopicListProperty, value); }
        }
        public static readonly DependencyProperty TopicListProperty =
            DependencyProperty.Register("TopicList", typeof(ObservableCollection), typeof(MainWindow), new UIPropertyMetadata(null));
    

    Edit 3

    To see changes in items

    1. implement INotifyPropertyChanged interface in CheckedListItem (each setter should call PropertyChanged(this, new PropertyChangedEventArgs()) event)
    2. or derive CheckedListItem from DependencyObject, and convert Name, ID, IsChecked to dependency properties
    3. or update them totally (topicList[0] = new CheckedListItem() { Name = ..., ID = ... })

提交回复
热议问题