Andrew Ahearne wrote:
> I have declared an image in a panel's resources
>
> <Image x:Key="Cut" Source="Resources\\Images\\cut.png"/>.
>
> I then used this image on a toolbar button and a menubar item.
>
> <Button Command="Cut" Content="{StaticResource Cut}"/> <MenuItem
> Command="Cut" Icon="{StaticResource Cut}"/>
>
> Whenever I open the menu in which the above menu item appears the
> image disappears from the button and never reappears. Is this a bug or
> am I doing something wrong as it seems it should be something that is
> possible?
Hmm... well the problem is this: Image is a FrameworkElement and FrameworkElement's
are only allowed to be in one part of the LogicalTree at a time (i.e. only
one parent). In your example, you're basically trying to put the Image into
two positions in the tree, hence the reason it dissapears when the MenuItem
is first shown (it's being reparented). The solution to your problem is simple:
don't resource the Image, instead resource the BitmapImage and assign that
to two diff. Image elements. It would go something like this:
<BitmapImage x:Key="CutImage" UriSource="Resources\\Images\\cut.png" />
<Button Command="Cut">
<Image Source="{StaticResource CutImage}" />
</Button>
<MenuItem Command="Cut" />
<MenuItem.Icon>
<Image Source="{StaticResource CutImage}" />
</MenuItem.Icon>
</Menu>
As to whether it should be a bug... yeah I guess it is. When they try to
expand the StaticResource for the MenuItem Icon they should be able to detect
that the Image element (or any FrameworkElement for that matter) is already
rooted someplace else in the tree and fire an error.
HTH,
Drew
___________________________________
Drew Marsh
Chief Software Architect
Mimeo.com, Inc. -
http://www.mimeo.com
Microsoft C# / WPF MVP
Weblog -
http://blog.hackedbrain.com/