1

I have a image control named "imgGameList" inside a LongListSelector DataTemplate which I would like to access from code, but I can't find the control from code.

My LongListSelector with my image control:

<phone:LongListSelector Name="llsGameList" ItemsSource="{Binding}" Tap="llsGameList_Tap" Margin="0,90,0,0">
    <phone:LongListSelector.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Image Name="imgGameList" Margin="0,10,0,10" Stretch="Fill" HorizontalAlignment="Left" VerticalAlignment="Top" Height="200" Width="150">
                    <Image.Source>
                        <BitmapImage UriSource="{Binding BoxArtFrontThumb}"
                                 CreateOptions="BackgroundCreation" DecodePixelHeight="200" DecodePixelWidth="150" />
                    </Image.Source>
                </Image>
            </Grid>
        </DataTemplate>
    </phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>

The reason why I'm trying to access the image control is because I'm facing memory issues with it and would like to apply gleb.kudr fix to it: Why do I get an OutOfMemoryException when I have images in my ListBox?

I hope there is someone that can help me. Thanks.

Community
  • 1
  • 1
Thunder
  • 117
  • 18

1 Answers1

0
    public FrameworkElement SearchVisualTree(DependencyObject targetElement, string elementName)
    {
        FrameworkElement res = null;
        var count = VisualTreeHelper.GetChildrenCount(targetElement);
        if (count == 0)
            return res;

        for (int i = 0; i < count; i++)
        {
            var child = VisualTreeHelper.GetChild(targetElement, i);
            if ((child as FrameworkElement).Name == elementName)
            {
                res = child as FrameworkElement;
                return res;
            }
            else
            {
                res = SearchVisualTree(child, elementName);
                if (res != null)
                    return res;
            }
        }
        return res;
    }

Usage:

Image image = SearchVisualTree(listItem, "imgGameList") as Image;
Ku6opr
  • 8,126
  • 2
  • 24
  • 31
  • Thanks. Can you tell me if this is more efficient than the fix the user pm_2 posted on this: http://stackoverflow.com/questions/15189715/how-to-access-a-control-inside-the-data-template-in-c-sharp-metro-ui-in-the-code – Thunder May 12 '14 at 20:12
  • I see the same approach – Ku6opr May 13 '14 at 05:53
  • Thanks. Its working fine, i now have access to my image control, but when i set the image source to null it is just the first image in my LongListSelector which gets removed?, do you know why?. For example: I have 20 images showing in my LongListSelector, when i press a button i want all the images to be removed to free up memory, but when i try this, its only the first image that gets removed and not the 19 other images, i hope you understand what im trying to do, in the end i want to free up memory from images when im not using them. Thanks. – Thunder May 13 '14 at 07:42
  • You should iterate through all items in the list and call `SearchVisualTree` on each item individually – Ku6opr May 13 '14 at 17:13
  • How can i get listitem, inside the selection_changed event. – Noorul Oct 16 '14 at 13:12