I have included an icon file inside my Resources.resx that I'd like to show on a TreeViewItem that is inside a stackpanel.
1)Can .ico files be used for this purpose? Or does it have to be .bmp or jpg?
2)What do you set the source as in XAML? The following code didn't work for me
<StackPanel Orientation="Horizontal">
<Image Margin="2" Source="/Resources/Folder_Back.ico" />
<TextBlock Margin="2" Text="{Binding ProgramName}"
Foreground="White" FontWeight="Bold"/>
</StackPanel>
First: Add resources rsx then: add images as images to the resource file and set the image build action to Resource. Now you can access the images like this:
you can't do that. that worked only in winforms
see this post for more info
Different way how add image to resources
use the method shown in this post
WPF image resources
instead
quote:
If you will use the image in multiple places, then it's worth loading the image data only once into memory and then sharing it between all
Image
elements.To do this, create a
BitmapSource
as a resource somewhere:Then, in your code, use something like:
In my case, I found that I had to set the
Image.png
file to have a build action ofResource
rather than justContent
. This causes the image to be carried within your compiled assembly.to make the solution of Qorbani work add a converter to the Image Source.Binding!
XAML - Namespaces
Xaml - Resource (UserControl or Window)
Xaml Code
BitmapToImageSourceConverter.cs
Here is a trick to access image in Resource file:
Accessing image from Resource File in XAML markup
First you need to add reference to project properties like this:
And then access it via XAML like this:
You can use PNG/JPG/BMP as well as ICO file but everyone recommend PNG.