I would like to be able to use vector graphics, preferably defined in XAML, as the Source of an Image control, just like I can currently use a raster image like a PNG. That way I could easily mix and match between bitmap and vector images, like this:
<StackPanel>
<Image Source="Images/Namespace.png"/>
<Image Source="Images/Module.xaml"/>
</StackPanel>
Module.xaml would most likely have <DrawingImage>
as its root element instead of <UserControl>
.
Actually, what I'm really going for is this, so my ViewModel could select either a raster or vector image at its discretion:
<Image Source="{Binding ImageUri}"/>
Is this possible? Can Image.Source load XAML classes from a given URI? Or is it only able to load bitmap resources?
1) Add the DrawingImage.xaml to the project and set its properties to 'BuildAction=Content' and 'Copy Always'. Or else you can dynamically load the XAML from outside since the logic I am going to explain will work for loose-xaml also.
2) Write a Converter to convert the XAML uri to UIELement, in your case it will be always DrawingImage
3) Write the XAML as below
Embed the XAML resource (DrawingImage) with type 'Resource'. It is then not a separate file and can be directly referenced via a URI, as in your original example -- BUT the URI is non-trivial. You have to figure out Microsoft's "pack" URI syntax and use that.
You can simply reference your vector graphics as StaticResources:
Store the images in a ResourceDictionary as DrawImage's. Expression Blend can help you generate this stuff: