Every developer has found him or herself in the situation where a control is missing the one feature that would be perfect in a specific situation or they would love to have a slightly different behavior of an existing feature. This is where attached properties come in handy.
Attached properties could be used to extend the functionality of a control with very little implementation on our side. In this blog post I will demonstrate how to implement some basic attached properties in specific scenarios to extend the functionality of a WPF / Silverlight control.
The next examples will demonstrate how to extend the functionality of RadListBox and RadDocking controls but you could use this approach in every WPF or Silverlight control.
The first example will focus on how to extend the functionality of RadListBox control. A very common scenario when using a ListBox control is when you would like to bind a collection of objects to the control and use it as a list of selected items. Accomplishing this task with the help of attached properties is a fast and simple task.
First we will need to create a helper class and in it create the attached property as a dependency property by defining a public static readonly field of type DependencyProperty. After that we will need to create a set and get methods that will write and return the data of the attached property. Next we will need to create the registered OnPropertyChanged method with the custom logic that will add each of the objects in the created dependency property List to the SelectedItems of the RadListBox. The next code snippet shows how this helper class should look like:
The full code can be found in the downloadable project.
All that is left is to declare your custom attached property in the xaml of the RadListBox and bind a collection of objects to it. The next code snippet shows how this should look like:
The Countries collection in the ViewModel and the SelectedCountries collection are sample ObservableCollections of custom Country objects that have Name property and the local namespace is the namespace of the custom helper class.
Now let us see how we can benefit from the use of attached properties in the RadDocking control. One common scenario with RadDocking is when you find yourself with the need to implement a MVVM pattern and set a collection of Panes as ItemsSource of a specific PaneGroup. Again achieving this with the help of attached properties is a simple task.
Next we will need to create an extension class for the RadPaneGroups (the PaneGroupExtensions class). In this class we will define the attached properties for the PaneGroupExtension and the ItemsSource.
Define a Group field of type RadPaneGroup and Panes field of type Dictionary<object, RadPane>. This fields will store the RadPaneGroup and RadPanes:
After that all that is left to do is define a field of type ObservableCollection<PaneModel> in the code behind and populate it with some sample data.
The xaml of the Docking control should look like this:
Subscribe to be the first to get our expert-written articles and tutorials for developers!