简体   繁体   English

C#Windows Phone从代码访问DataTemplate内部的图像控件

[英]C# Windows Phone access image control inside DataTemplate from code

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. 我想从代码中访问LongListSelector DataTemplate中有一个名为“ imgGameList”的图像控件,但无法从代码中找到该控件。

My LongListSelector with my image control: 我的LongListSelector和我的图像控件:

<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? 之所以尝试访问图像控件,是因为我面临着与此相关的内存问题,并且想对其应用gleb.kudr修复程序: 为什么在ListBox中有图像时为什么会出现OutOfMemoryException?

I hope there is someone that can help me. 我希望有人可以帮助我。 Thanks. 谢谢。

    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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C#背后的代码中访问datatemplate内部的网格控件名称 - how to access the grid control name inside the datatemplate in code behind c# C#-来自后面代码的WPF访问DataTemplate中的元素 - C# - WPF Access elements in a DataTemplate from code behind 从代码访问数据模板(列表视图)的控件 - Access a control of a datatemplate (listview) from code 如何访问Windows 8.1存储中的Hubsection Datatemplate内的任何控件 - How to access any control inside Hubsection Datatemplate in Windows 8.1 store 更新Windows Phone 8 C#上列表框中特定项目的DataTemplate - Update DataTemplate for a specific item on a listbox on Windows Phone 8 C# C#Windows Phone 7:通过DataTemplate创建的动画ListBoxItems - C# windows phone 7: Animating ListBoxItems created via DataTemplate 如何在FlipView中访问XAML DataTemplate内部的图像控件 - How do I access a image control inside a XAML DataTemplate in a FlipView 列表框C#中的访问控制(Windows Phone 8) - Access controls inside listbox c# (windows Phone 8) 如何访问位于DataTemplate标签的文本框控件 <phone:PanoramaItem.HeaderTemplate> 从代码开始-在后面(MainPage.xaml.cs) - How to access textbox control located in DataTemplate tags of <phone:PanoramaItem.HeaderTemplate> from code - behind (MainPage.xaml.cs) 在Windows Phone 7中访问数据模板中的元素 - Accessing elements inside a datatemplate in Windows Phone 7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM