Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

3. Advanced Data Binding > Building a change-aware collection type - Pg. 127

Chapter 3 Custom markup extensions are useful in many scenarios. For example, we can wrap functionality inside an extension, for which we would normally have to expose extra properties from our data context. Also, functionality that we would normally add using a converter can be wrapped in a markup extension. Finally, we also get access to static classes from XAML, which isn't possible otherwise. This can be useful to access classes such as service locators which are often static. Creating a custom markup extension is possible by inheriting from the MarkupExtension class. In our extension, we then have to override the ProvideValue() method. This method, as the name implies, returns an object for the value of the property where we are using the extension in XAML. A custom markup extension can have parameters. We can create parameters by creating one or more public properties on the extension class. From XAML, these are then available within the markup extension. In the following code, both Key and Default are parameters. Text="{extensions:TranslatorExtension Key=ApplicationName, Default=Welcome}" One important note for WPF developers is that positional parameters are not available in Silverlight's implementation of custom markup extensions. This means that we always have to specify the name of the parameter; we can't fall back on a default property. Building a change-aware collection type Applies to Silverlight 3, 4 and 5