Windows Vista Forums

Is this a bug?
  1. #1


    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

  2. #2


    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

  3. #3


    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

  4. #4


    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

Is this a bug? problems?