繁体   English   中英

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

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

我想从代码中访问LongListSelector DataTemplate中有一个名为“ imgGameList”的图像控件,但无法从代码中找到该控件。

我的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>

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

我希望有人可以帮助我。 谢谢。

    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;
    }

用法:

Image image = SearchVisualTree(listItem, "imgGameList") as Image;

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM