WPF Menu Icon Binding

  • 1081
  • 0
  • 2013-07-01

摘要:WPF Menu Icon Binding

終於找到 Menu Icon 的 Binding 方法了!

原文出自:http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/71cd9782-8fd3-4094-89ce-8606cd05db4f

因為用 Binding ,所以需要用 Converter 把圖轉換一下,

以下是我的code:

public class MyImageConverter:IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {

            if (value == null)
            {
                return null;
            }
            else
            {
                string filefullname = @"E:\Images\Koala.jpg";
                if (File.Exists(filefullname))
                {
                    BitmapImage bImg = new BitmapImage();
                    bImg.BeginInit();
                    bImg.UriSource = new Uri(filefullname);
                    bImg.EndInit();
                    
                    Image img = new Image();
                    img.Source = bImg;
                    return img;
                }
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }