Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > Avalon

Is this a bug?

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 04-11-2006   #1 (permalink)
Andrew Ahearne
Guest


 

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
Guest


 

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
Guest


 

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
Guest


 

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
Closed Thread

Thread Tools
Display Modes




Update your Vista Drivers Update Your Vista Drivers Now!!

Vistax64.com 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 2005-2008
Page generated in 0.25294 seconds with 9 queries