1

I would like to use the same icon for my main window and for any dialogs or message boxes whithin my application, so I tried to set it like this in a ResourceDictionary:

<Style TargetType="{x:Type Window}">
  <Setter Property="Icon" Value="pack://application:,,,/MyReferenceAssemblyName;component/Images/myIcon.gif"></Setter>
</Style>

But that does not work. How could I share the same icon with the different windows?

Edit:

I have a simple resource dictionary (Style.xaml) where I am defining some global settings. I use it in my App.xaml like this:

<Application.Resources>
  <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="Resources/ViewModelTemplates.xaml"/>
      <ResourceDictionary Source="Resources/Style.xaml"/>
    </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
</Application.Resources>

The file contains some definitions like e.g. button height, text box foreground color, etc. There is no problem with those and all the panels and window my application creates use these settings. That is why I would like the icon to be defined there as well, to have it used allover the application.

I am not looking for a way to set the icon of the .exe file.

Edit:

I have not found the solution for what I want to do, so I ended up creating a BitmapImage in my ResourceDictionary and use it as DynamicResource in each of my Window-Classes.

<BitmapImage x:Key="ApplicationIcon" UriSource="pack://application:,,,/MyReferenceAssemblyName;component/Images/myIcon.gif"></BitmapImage>

and

<Window ...        
    Closing="Window_Closing"
    Title="{Binding Title, UpdateSourceTrigger=PropertyChanged}"
    IsEnabled="{Binding IsEnabled, FallbackValue=True, Mode=OneWay}" 
    WindowState="Maximized"
    Icon="{DynamicResource ApplicationIcon}">
    ...
</Window>
tabina
  • 1,095
  • 1
  • 14
  • 37
  • Does it not work at all, or does it work in some places? – Surfbutler Jul 06 '12 at 13:35
  • No, it does not work at all, not even the main window displays the icon. Setting the icon in the main window's code behind using the same pack URI works fine though. – tabina Jul 06 '12 at 13:44
  • Which "ResourceDictionary" are you using? Do you include it in all of your Windows? Note that system dialogs and message boxes will not use your icon - only those that you create. – Wonko the Sane Jul 06 '12 at 15:01
  • I edited my question above. The resource dictionary is included in my app.xaml. – tabina Jul 09 '12 at 13:47

1 Answers1

1

For others that come across this issue, as I had the same one, see a similar question which provides a bit more of an explanation to why this doesn't work. How to set default WPF Window Style in app.xaml?

There's also a couple of suggestions; one being tabina's solution and to apply the style to each Window separately. It includes an interesting approach to deriving the Window class, but it's down to personal preference, as they're only work-arounds.

Community
  • 1
  • 1
millzy
  • 11
  • 1