2

I'd like to create my custom error/exception dialog with the standard Windows "Error icon".

I followed the advice from this question and it works.

However I'm currently creating an instance property I bind to just like to any property:

class ErrorWindowViewModel
{
    private readonly ImageSource _errorImage;

    public ImageSource ErrorImage { get { return _errorImage; } }

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

What I'd like to do is to have a static field defined in my ErrorWindow class:

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

I can't force my XAML to reference that field.

<Image Source="what_to_put_here_to_make_it_work" />

I'm using WPF 4.5.

Community
  • 1
  • 1
dzendras
  • 4,721
  • 1
  • 25
  • 20

1 Answers1

3

You would have to create a static property

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

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

and bind like this:

<Image Source="{Binding Source={x:Static local:ErrorWindow.ErrorImage}}"/>
Clemens
  • 123,504
  • 12
  • 155
  • 268