访问XAML DataTemplate中的子项

时间:2015-09-22 11:03:57

标签: listview c#-4.0 itemcontainergenerator

我使用列表视图,在列表视图中是一个图像,从天蓝色的斑点中检索到的图像。如何访问列表视图中的图像控件以获取其地址   在Windows Phone 8.1上

<ListView Name="list" ItemsSource="{Binding}" SelectionChanged="selection_changed" >

        <ListView.ItemTemplate>
            <DataTemplate x:Name="data">
                <Grid x:Name="gridtempl">

                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>

                    <Image x:Name="img" Source="{Binding URL}" HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="200" Grid.Column="0" Stretch="UniformToFill" Margin="10,0,0,0"    />
                    <TextBlock x:Name="name1"  Text="{Binding name}" TextWrapping="NoWrap" Grid.Column="1"/>
                    <TextBlock  Name="desc" Text="{Binding description  }"  TextWrapping="NoWrap" Grid.Column="1"/>
                </Grid> 
            </DataTemplate>

        </ListView.ItemTemplate>
    </ListView>


 private async void selection_changed(object sender, SelectionChangedEventArgs e)
        {


            if (list.SelectedItem == null)
            {
                var msg1 = new MessageDialog("hhh");
                msg1.ShowAsync();

            }
            var _Container = list.ItemContainerGenerator.ContainerFromItem(list.SelectedItem);
            var _Children = AllChildren(_Container);

            //var _FirstName = _Children
            //    // only interested in TextBoxes
            //    .OfType<Image>()
            //    // only interested in FirstName
            //    .First(x => x.Name.Equals("img"));
            var imageControl = _Children.OfType<Image>().First(c => c.Name.Equals("img"));





        }


public List<Control> AllChildren(DependencyObject parent)
    {
        var _List = new List<Control>();
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
        {
            var _Child = VisualTreeHelper.GetChild(parent, i);
            if (_Child is Control)
                _List.Add(_Child as Control);
            _List.AddRange(AllChildren(_Child));
        }
        return _List;
    }

0 个答案:

没有答案