tags:

views:

541

answers:

2

I'm looking to use some icons from the VS2008ImageLibrary/Actions/pngformat in my WPF application to get a more Visual Studio look and feel - like the "Insert Standard Items" command in WinForms.
I've added the images as resources, and referenced them in my xaml file:

<DockPanel.Resources>
        <Image x:Key="NewIcon" Source="Toolbar Images/NewDocumentHS.png" Height="16" Width="16"/>
</DockPanel.Resources>

And then I've added the image to the Menu and Toolbar

<MenuItem Command="ApplicationCommands.New" Icon="{StaticResource NewIcon}"/>
<!-- ... -->
<Button Command="ApplicationCommands.Save"><StaticResourceExtension ResourceKey="SaveIcon"/></Button>

However, when I open the menu, the icon disappears from the Toolbar, as if the button was never there.I've read elsewhere that this kind of thing occurs due to .NET's lack of support for Vista Icons/Compressed PNGs, but I'm not sure this is exactly the same problem. My attempts to work around this by changing formats have failed, or given sub-optimal results (such as losing transparency). Does anyone know of a better way to do this?

Edit: For other people having this issue, making duplicate entries with different keys in the < DockPanel.Resources > tag for each reference seems to be a functional enough workaround, though I'd be concerned that the images might disappear again if they need to be reloaded for any reason.

+1  A: 

Use the format that loses the transparency and set the transparency key to that background color.

Lucas McCoy
The issue with using formats without transparency is that I lose the anti-aliasing on the images. Moreover, this requires modifying all of the icons to ensure that the background doesn't match any other color in the palette. It might work, but I'm really hoping for a more elegant solution.
T.R.
Yeah this solution is good if your just wanting to make a brute force app, but since this is not what your wanting and you haven't gotten any answers so far, I would put up a bounty.
Lucas McCoy
+1  A: 

Hi, T.R.!

I think you have to define a separate resource for usage with the Button:

    <DockPanel.Resources>
        <Image x:Key="NewIconForMenu" Source="Toolbar Images/NewDocumentHS.png" Height="16" Width="16"/>
<Image x:Key="NewIconForButton" Source="Toolbar Images/NewDocumentHS.png" Height="16" Width="16"/>
</DockPanel.Resources>

I suppose, when you declare an Image resource it is basically creating object of type Image. After that you make it a child of the MenuItem and the Button by setting Icon and Content properties, therefore it is not a tree anymore. This is not legal.

If you try to declare the second button that also uses NewIcon, you'll get an exception of logical tree.

I don't know how MenuItem escaped that exception, may be it is smart enough to remove the Image element from Button's children (this is how it disappears) before it adds the image to own logical children collection.

Okay, I just found a simpler solution. You could add x:Shared="false" to the image declaraiton in the dictionary. This will ensure that a new instance of that image is created and it would not break wpf tree rules.

Ah, that makes a lot of sense.x:Shared="false" is exactly the kind of solution I was looking for. Thank you very much.
T.R.