Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > Avalon

Vista - Is this a bug?

 
 
Old 04-11-2006   #1 (permalink)
Andrew Ahearne


 
 

Is this a bug?

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?

Thanks,
Andrew


My System SpecsSystem Spec
Old 04-11-2006   #2 (permalink)
Drew Marsh


 
 

Re: Is this a bug?

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/


My System SpecsSystem Spec
Old 04-11-2006   #3 (permalink)
Andrew Ahearne


 
 

Re: Is this a bug?

"Drew Marsh" <drub0y@hotmail.no.spamming.com> wrote in message
news:f01844f1f0a408c82b82bbd12646@msnews.microsoft.com...
> 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/
>
>


Drew, thanks for the help. Your answer makes sense. I should've remembered
that Image was a FrameworkElement and what that entails but when no error
was fired I was thrown off. It's a shame though that you can't declare
reuasble content like this though as declaring a BitmapImage and using it as
the Source of the Image really isn't saving you much over using the path of
the image file directly.

Thanks again,
Andrew

My System SpecsSystem Spec
Old 04-11-2006   #4 (permalink)
Drew Marsh


 
 

Re: Is this a bug?

Andrew Anhearne wrote:

> It's a shame though that you
> can't declare reuasble content like this though as declaring a
> BitmapImage and using it as the Source of the Image really isn't
> saving you much over using the path of the image file directly.


Well keep in mind the most important thing it's saving you is runtime resources.
That that image is only loaded once into memory and even if it's referenced
by 3000 elements there's very little additional cost compared to loading
it for each one. Also, if you were to manipulate the BitmapSource at runtime,
all referencing <Image> elements would be updated in one shot.

Cheers,
Drew
___________________________________
Drew Marsh
Chief Software Architect
Mimeo.com, Inc. - http://www.mimeo.com
Microsoft C# / WPF MVP
Weblog - http://blog.hackedbrain.com/


My System SpecsSystem Spec
 

Thread Tools



Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46