4

In XAML, I am attempting to bind the window/application icon to an icon on the file system.

In Window.xaml

Icon="{Binding ApplicationIcon}"

In AppViewModel.cs

public ImageSource ApplicationIcon
{
  get
  {
    return new BitmapImage(new Uri(pathReadFromConfigFile));
  }
}

When I do this, the icon is shown but it is not transparent. However, if I set the icon within the project (not using binding), the icon is added to the project and it is transparent when I start the application. Why is it acting differently between these two scenarios?

bsh152s
  • 3,178
  • 6
  • 51
  • 77
  • 2
    Forgive me, I don't mean to divert from the original question, but isn't it taking MVVM to the extreme? Unless your icon will change at run time, hence, the decision-making needs to be done in VM and exposed via property so that the View is passive..Otherwise, why bother? – denis morozov Apr 13 '12 at 16:59
  • 1
    I understand what you're saying, but we're trying to design an application that is configurable. We have several very similar applications but their icons are different. Our goal is to have one executable using different config files. – bsh152s Apr 13 '12 at 17:07
  • 1
    yeah, makes sense, the reason I asked -- I have that internal fight between being a purist vs. a getter-done. So I am just wondering what other people's motivations are. Thanks for reply. – denis morozov Apr 13 '12 at 17:17
  • It must be an issue with how the image is loaded and the ImageSource is created. If you bind your ApplicationIcon to an Image.Source with strange background color, do you see transparency? Show us the rest of your code. – Brannon Apr 14 '12 at 15:30
  • @Brannon, I added more code. I'm guessing it has something to do with returning a BitmapImage. But, how else can I create the image? – bsh152s Apr 19 '12 at 19:31
  • @bsh152s: Your code works on my system with any icon (regardless of size from 16x16 to 48x48 and color depth from 4-bit to 32-bit). Using VS2010 Premium SP1 (10.0.40219.1), targeting .NET 4.0. Could it be caused by an older/non-SP1 VS version? – Alan Apr 19 '12 at 20:05

1 Answers1

1

Figured it out. It was the BitMapImage creation causing the problems. Using BitmapFrame now instead.

public ImageSource ApplicationIcon
{
  get
  {
    return BitmapFrame.Create(new Uri(pathReadFromConfigFile));
  }
}
bsh152s
  • 3,178
  • 6
  • 51
  • 77