Hi Greg,

 

I’m going to make a few assumptions here.. First up is that the “image” is a 
Path element defined in a resource dictionary which is then inside the xaml 
file. 

 

Next up, the path element will need to have a name defined using 
x:key=”nameofresourcehere”

 

<ResourceDictionary>

    <Path x:Key=”MyXamlImage” …. />

</ ResourceDictionary >

 

 

Once you have this, then in the xaml file you want the image to be included in, 
you’ll need to import the resource, it’s a bit verbose but it will be along the 
lines of

 

<Window.Resources>

    <ResourceDictionary>

        <ResourceDictionary.MergeDictionary>

            <ResourceDictionary Source=”PathToFileWithImage.xaml”/>

        </ResourceDictionary.MergeDictionary>

    </ResourceDictionary>

</Window.Resources>

 

Then to use it,

<MenuItem…. Icon=”{StaticResource MyXamlImage}”>

</MenuItem>

 

Hope this helps.

 

Ed.

 

From: Greg Keogh via ozdotnet <ozdotnet@ozdotnet.com> 
Sent: Sunday, July 28, 2024 5:57 PM
To: ozDotNet <ozdotnet@ozdotnet.com>
Cc: Greg Keogh <gfke...@gmail.com>
Subject: XAML images in WPF?

 

Folks, in a video I saw a few weeks ago I learned that the Visual Studio team 
uses XAML for most of their images, not png or similar. I'm keen to do the same 
because I imagine that I would no longer need tedious sets of 16x16 32x32 48x48 
... etc images because each XAML file is composed from scalable geometry 
elements that would look good at any size.

 

I downloaded the Image Library 
<https://www.microsoft.com/en-us/download/details.aspx?id=35825>  which 
contains thousands of XAML, png and svg sets of standard images. But I can't 
figure out how to simply use the XAML files in a WPF app. All the examples I 
can find seem to have pasted the contents of the XAML images into resource 
files and then reference them by x:Key. I don't want to manually paste XAML 
around like that, I just want to add a file like FolderClosedBlue.xaml (for 
example) to my project and use it as the icon for a menu or button as simply as 
possible. How can I replace this sort of thing?...

 

<MenuItem Header="_Foo Command" Command="...">

  <MenuItem.Icon>

    < use the XAML image file somehow instead of a resource png? >

  </MenuItem.Icon>

</MenuItem>

 

Is there some coding trick I'm not aware of to simply use an image defined in a 
XAML file in my app?

 

Cheers, Greg K

-- 
ozdotnet mailing list 
To manage your subscription, access archives: https://codify.mailman3.com/ 

Reply via email to