Sunday, 5 May 2013

Data Binding a Static Resource in WPF

I was recently trying to create a data template for data binding a list of objects, each one defining an icon to display from MahApps.Metro.Resources Icons.xaml. I found a solution by Pedro Sampaio to extend StaticResourceExtension to make it bindable, but I was having some issues such as not being able to dynamically change the colour of the icons, or use the same icon multiple times.

I decided to go for a simpler solution and create a converter which would convert a string, being the resource key, to the icon's geometry. The converter has to make some assumptions to the location of the Icons.xaml file and the structure of its contents. The converter caches the icons geometry once accessed.



The objects being databound have a string property for the resource key of the icon to use, and this is bound to the a Path's Data property in the data template, which allows the Fill property to be changed dynamically.

No comments:

Post a Comment