亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何用Properties.Resources中的圖像從WPF中的代碼隱藏動態更改圖像源?

如何用Properties.Resources中的圖像從WPF中的代碼隱藏動態更改圖像源?

縹緲止盈 2019-07-26 15:14:19
如何用Properties.Resources中的圖像從WPF中的代碼隱藏動態更改圖像源?我有一個WPF應用程序,需要向用戶提供關于內部狀態的反饋。設計有三張圖片,分別叫紅色、黃色和綠色。這些圖像中的一個將根據狀態一次顯示。以下是要點:這三個圖像在Properties.Resources中一次只顯示一幅圖像。狀態更改來自代碼隱藏中的進程,而不是用戶。我想綁定一個圖像控件,這樣我就可以從代碼背后更改圖像。我假設需要一個圖像轉換器將JPG圖像更改為圖像源,如:[ValueConversion(typeof(System.Drawing.Bitmap), typeof(ImageSource))]public class BitmapToImageSourceConverter : IValueConverter{     public object Convert(object value, Type targetType, object parameter, CultureInfo culture)     {         var bmp = value as System.Drawing.Bitmap;         if (bmp == null)             return null;         return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(                     bmp.GetHbitmap(),                     IntPtr.Zero,                     Int32Rect.Empty,                     BitmapSizeOptions.FromEmptyOptions());     }     public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)     {         throw new NotSupportedException();     }}我傾向于在初始化期間轉換一次圖像,并保留圖像源列表。我還假設需要一個依賴項屬性來將控件綁定到,但我不知道如何使用以下圖像源列表設置該屬性:    // Dependancy Property for the North Image     public static readonly DependencyProperty NorthImagePathProperty         = DependencyProperty.Register(             "NorthImagePath",             typeof(ImageSource),             typeof(MainWindow),             new PropertyMetadata("**Don't know what goes here!!!**"));     // Property wrapper for the dependancy property     public ImageSource NorthImagePath     {         get { return (ImageSource)GetValue(NorthImagePathProperty); }         set { SetValue(NorthImagePathProperty, value); }     }
查看完整描述

2 回答

?
紅糖糍粑

TA貢獻1815條經驗 獲得超6個贊

這應該也適用于PNG文件。也許您不應該將它們作為資源添加到您的項目中(請參閱我的編輯)。只需使用您最喜歡的圖像工具創建文件,并將它們添加到項目中的文件夾中即可??匆?/trans>這里關于接受。



查看完整回答
反對 回復 2019-07-27
  • 2 回答
  • 0 關注
  • 570 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號