How to Implement a ListBox of Checkboxes in WPF?

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

    Others have already made useful suggestions (use an observable collection to get list-change notification, make the collection a property rather than a field). Here are two they haven't:

    1) Whenever you're having a problem with data binding, look in the Output window to make sure that you're not getting any binding errors. You can spend a lot of time trying to fix the wrong problem if you don't do this.

    2) Understand the role change notification plays in binding. Changes in your data source can't and won't get propagated to the UI unless the data source implements change notification. There are two ways to do this for normal properties: make the data source derive from DependencyObject and make the bound property a dependency property, or make the data source implement INotifyPropertyChanged and raise the PropertyChanged event when the property's value changes. When binding an ItemsControl to a collection, use a collection class that implements INotifyCollectionChanged (like ObservableCollection), so that changes to the contents and order of the collection will get propagated to the bound control. (Note that if you want changes to the items in the collection to get propagated to the bound controls, those items need to implement change notification too.)

提交回复
热议问题