Data binding allows you to establish a link between the UI and the underlying business logic and to keep them synchronized. This means that when a value is changed in the business layer, that change is automatically populated to the UI and vice versa. Of course, in order for this to work, you have to implement the proper notification or to use objects that have already implemented it.
Binding to RadContextMenu involves the following property:
- RadContextMenu.ItemsSource - gets or sets the data source (IEnumerable) used to generate the content of the RadContextMenu control. It can be bound to data from a variety of data sources in the form of common language runtime (CLR) objects.
You can bind RadContextMenu to the following types of data sources:
IEnumerable - supports simple iteration of a collection. See the MSDN article for more information.
To bind the RadContextMenu to a collection of business objects, you should use its ItemsSource property. If you want the changes to the collection to be automatically reflected to the RadMenuItems, the collection should implement the INotifyCollectionChanged interface. There is a build-in collection in WPF, which implements the INotifyCollectionChanged interface and you could use it without making any effort - this is the generic ObservableCollection
Consider using ObservableCollection
or one of the other existing collection classes like List , Collection , instead of implementing your own collection. If the scenario requires a custom collection to be implemented, use the IList interface, which provides individual access by index to its items and the best performance.
The ItemsSource property allows the RadContextMenu to be bound to any collection that implements the IEnumerable interface. For each item in the collection, a container of type RadMenuItem is created. By using the ItemTemplate, ItemContainerStyle and TemplateSelectors you can control the appearance of the dynamically created items.
Besides the RadContextMenu's ItemTemplate property, you could use the DisplayMemberPath property for controlling the appearance of the created items.
If neither the DisplayMemberPath nor the ItemTemplate are set, then the content of the item would be set to the value returned by the ToString() method of the business object.