I'm using a Viewbox
to create a set of icons that I will dynamically bind to a WPF view.
I'm binding to the resource name and using a Converter
to convert the resource name to an ImageSource
.
I know how to do it if the resource is a Path
, but how to do it with a Viewbox
?
This is how I convert the resource name, if the resource is a Path
, to an ImageSource
:
public class ResourceNameToImageSourceConverter : BaseValueConverter {
protected override ImageSource Convert(string value, System.Globalization.CultureInfo culture) {
var resource = new ResourceDictionary();
resource.Source = new Uri("pack://application:,,,/MyAssembly;component/MyResourceFolder/ImageResources.xaml", UriKind.Absolute);
var path = resource[value] as Path;
if (path != null) {
var geometry = path.Data;
var geometryDrawing = new GeometryDrawing();
geometryDrawing.Geometry = geometry;
var drawingImage = new DrawingImage(geometryDrawing);
geometryDrawing.Brush = path.Fill;
geometryDrawing.Pen = new Pen();
drawingImage.Freeze();
return drawingImage;
} else {
return null;
}
}
}
And this is what the Viewbox declaration looks like.
<Viewbox> <Viewbox> <Grid> <Path> ... </Path> <Path> ... </Path> <Path> ... </Path> <Rectangle> ... </Rectangle> </Grid> </Viewbox> </Viewbox>