如何使用Properties.Resources中的圖像從WPF中的代碼隱藏動(dòng)態(tài)更改圖像源?我有一個(gè)WPF應(yīng)用程序,需要向用戶提供有關(guān)內(nèi)部狀態(tài)的反饋。設(shè)計(jì)是有三個(gè)圖像,稱為紅色,黃色和綠色。其中一個(gè)圖像將根據(jù)狀態(tài)一次顯示。以下是要點(diǎn):這三個(gè)圖像位于代碼隱藏中的Properties.Resources中一次只能顯示一個(gè)圖像。狀態(tài)更改來自代碼隱藏中的進(jìn)程,而不是來自用戶。我想綁定一個(gè)圖像控件,以便我可以從代碼隱藏更改圖像。我假設(shè)我需要一個(gè)圖像轉(zhuǎn)換器來將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();
}}我更喜歡在初始化期間轉(zhuǎn)換圖像一次并保留圖像源列表。我還假設(shè)我需要一個(gè)依賴屬性來綁定控件,但我不知道如何使用這個(gè)圖像源列表來設(shè)置它: // 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); }
}
如何使用Properties.Resources中的圖像從WPF中的代碼隱藏動(dòng)態(tài)更改圖像源?
一只斗牛犬
2019-07-25 10:07:42