views:

7978

answers:

2

Hi guys, What I am trying to do is so simple but I am having a hard time making it work. I saw some posts along the same lines but I still have questions.

I have a MenuItem object called mnuA. All I want is set the icon property programatically in C#. I have tried the following

a) mnuA.Icon = new BitmapImage{UriSource = new Uri(@"c:\icons\A.png")}; Results: Instead of showing the actual icon, I get the class name (System.Windows.Media.Imaging.BitmapImage)

b) mnuA.Icon = new BitmapImage(new Uri(@"c:\icons\A.png")); Results: Instead of showing the actual icon, I get the path of the image (file:///c:/icons/A.png)

What am I doing wrong? Do I really need a converter class for something simple like this?

Thanks

+4  A: 

Try this:

Image img = new Image();
img.Source = new BitmapImage(new Uri(@"c:\icons\A.png"));
mnuA.Icon = img;
w4g3n3r
A: 

Might be a long shot, but try something like:

Uri u = new Uri(...); mnuA.Icon = new BitmapImage(u);

What it seems its happening is that your icon is getting converted to a string.

Artur Carvalho