0

I want to implement a WPF control which supports binding to a collection of data items that implement a particular interface supports templates for them as well and arranges these items according to the properties exposed in the interface. Is it generally an acceptable practice? And which strategy is better to use?

Thanks!

user3060586
  • 133
  • 5

1 Answers1

0

The information you have supplied is simply too broad, so I'll try to cover the basics only.

Yes, WPF does support binding controls to collections. You can bind to any collection that implements IEnumerable. This means you can bind to most collection types available in .NET including List<T>.

If you want an active binding, i.e. the control should update itself as you add or remove items from the underlying collection, your collection must implement change notification. You can either implement INotifyPropertyChanged or simply use ObservableCollection<T> for this purpose.

At the front-end, you can use ListBox if you want to be able to select items, or ItemsControl if you don't.

You must create DataTemplates against your underlying data types and assign them to ItemsControl.ItemTemplate or ListBox.ItemTemplate for your items to take appropriate look within the control.

Do note that your data type must implement change notification at its own level if you want to update item properties on the fly and want ItemsControl or ListBox to update its items accordingly. You can either implement INotifyPropertyChanged or derive your class from ObservableObject (in case you're using MVVM Light) for this purpose.

To change the way items are arranged within ItemsControl, you can set ItemsControl.ItemsPanel to one of the panels available in WPF, such as StackPanel, WrapPanel or Canvas etc.

Generally accepted practice is to divide your project into (at least) two layers (or projects in terms of Visual Studio); the underlying ViewModel that handles business logic and abstract storage of data and operations. This ViewModel does not know and is therefore independent of the UI. It is here that you implement change notification on your classes and collections and expose public properties that you want your UI controls to bind to. Then on top of the ViewModel layer, you create a View layer that knows about the ViewModel layer (you add reference of the ViewModel project in the View project) and then set DataContext of your controls to your ViewModel objects and bind their properties to the exposed properties of your ViewModel objects.

There is enormous amount of details involved in binding that you will discover and need to understand as you learn WPF, but this as I said is the very basic line of action that you should take.

dotNET
  • 33,414
  • 24
  • 162
  • 251
  • Thank you for you reply! I'm currently considering using of ItemsControl with Canvas as a panel, the main issue for me is how to arrange elements inside the canvas according to the data that is bound to these elements. – user3060586 Nov 02 '17 at 07:55
  • Is there a point where I can hook any possible insertion to Canvas to further obtain element's DataContext to process it and position accordingly? – user3060586 Nov 02 '17 at 08:15
  • Or should I implement a custom panel for this purpose? – user3060586 Nov 02 '17 at 08:34
  • The question is vague I know, sorry for that. To narrow it down I'm familiar with WPF basics, data binding, MVVM approach etc, the issue for me is the strategy in this particular case when elements are needed to be arbitrary arranged in some panel (Canvas or custom I don't know) according to their DataContext. – user3060586 Nov 02 '17 at 08:53
  • Your question is vague, but your problem is not. Would have been far easier for us if you had included this detail in the original post. Any way now that we know what you want, see the answer pointed to by Clemens. That handles exactly the situation you're handling. – dotNET Nov 02 '17 at 09:26
  • Sorry again. At that point of time I was very unsure whether ItemsControl and Canvas fit my case therefore I decided to formulate the question more general. I've implemented what Clemens suggested with setters in ItemContainerStyle which I was not aware about. This approach looks consistent works well and seems to fit my scenario well. Thanks to you and to Clemens! – user3060586 Nov 02 '17 at 12:03