2008-12-10 51 views
1

我想要做的是如此簡單,但我很難讓它工作。我沿着相同的路線看到了一些帖子,但我仍然有疑問。在WPF中通過C#顯示位圖圖像

我有一個名爲mnuA的MenuItem對象。我想要的是在C#中以編程方式設置圖標屬性。我曾嘗試以下

一)mnuA.Icon = new BitmapImage{UriSource = new Uri(@"c:\icons\A.png")}; 結果:相反,顯示實際圖標,我得到的類名(System.Windows.Media.Imaging.BitmapImage

B)mnuA.Icon = new BitmapImage(new Uri(@"c:\icons\A.png")); 結果:相反,顯示實際圖標,我得到的(file:///c:/icons/A.png

我在做什麼錯?我真的需要一個類似這樣簡單的轉換器嗎?

感謝

回答

6

試試這個:

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

可能是一個長鏡頭,但你可以試試:

烏里U =新的URI(...); mnuA.Icon = new BitmapImage(u);

看來它發生的是你的圖標正在轉換爲字符串。