2013-02-22 28 views
2

我想用標準Windows「錯誤圖標」創建自定義錯誤/異常對話框。如何綁定到靜態BitmapSource?

我遵循this question的建議,它的工作原理。

但我目前正在創建一個實例屬性我綁定到就像任何財產:

class ErrorWindowViewModel 
{ 
    private readonly ImageSource _errorImage; 

    public ImageSource ErrorImage { get { return _errorImage; } } 

    public ErrorWindowViewModel() 
    { 
     _errorImage = Imaging.CreateBitmapSourceFromHIcon(SystemIcons.Error.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); 
    } 
} 

我希望做的是有一個靜態字段我ErrorWindow類中定義:

partial class ErrorWindow : Window 
    { 
     private readonly static ImageSource ErrorImage = 
      Imaging.CreateBitmapSourceFromHIcon(SystemIcons.Error.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); 
    } 

我不能強制我的XAML引用該字段。

<Image Source="what_to_put_here_to_make_it_work" /> 

我正在使用WPF 4.5。

回答

3

你必須創建一個靜態屬性

private static readonly ImageSource errorImage = 
    Imaging.CreateBitmapSourceFromHIcon(SystemIcons.Error.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); 

public static ImageSource ErrorImage 
{ 
    get { return errorImage; } 
} 

並結合這樣的:

<Image Source="{Binding Source={x:Static local:ErrorWindow.ErrorImage}}"/>