Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference an icon resource file reference in XAML

I'm trying to get a button to display an icon in XAML. Initially I wanted to paste a character instead, but vb Express 2008 would not recognise it being a symbol as it is not a regular character. Then I created an icon and tried to set the button image property to reference it, but this does not exist, so more misery. Now I've used the project resources to add the icon file which it has shoved in the project resources directory.

What I want to do is use XAML to display the icon, in this tag:

<Button>
<Image Source="{StaticResource UpArrow}"/>
</Button>

I ideally want to have the image in a separate file and reference it by the project. I am a bit confused by the content/resource issue.

I tried to access it using:

<UserControl.Resources>
<BitmapImage x:Key="UpArrow" _
  UriSource="Resources/_25B2_Triangle__black__up_pointing.ico" />
</UserControl.Resources>

I've read some stuff about the pack assembly which is massive and confusing. Surely, putting an icon into the resources file should make it easy to reference using /Resources or similar, instead of the absolute file path, otherwise, what is the point of it?

I am wanting to do it all in XAML rather than code-behind because then this work would all be done by the client, but maybe I am not undestanding when to use code behind. With WPF, there is very little written about what you are trying to do at the large scale and why you would want to do it, apart from MS constant refrain of how 'cool' it is, -which is not very helpful.

Thanks..

like image 305
user326775 Avatar asked Jul 01 '10 14:07

user326775


1 Answers

The easiest way to do this is:

<Button>
<Image Source="Resources/_25B2_Triangle__black__up_pointing.ico"/>
</Button>

The important thing to remember is to make sure that your icon file is recognized as a valid Resource by your application. With WPF, there is one more step you need to take versus the old winforms way of doing it.

In your Solution Explorer, expand the 'Resources' folder, then click on the "_25B2_Triangle_black_up_pointing.ico" file. Then press F4 for properties. Make sure that the 'Build Action' property is set to 'Resource'.

like image 151
Stewbob Avatar answered Sep 22 '22 12:09

Stewbob