Getting UI Element from list view item template

Getting UI Element from list view item template

Problem Description:

I have ListView in my app that have style.

<ListView x:Name="FoldersListView"
          Style="{StaticResource FoldersListViewStyle}"
          ItemsSource="{Binding Source={StaticResource Locator}, Path=MainViewModel.Folders}"
          SelectedItem="{Binding Source={StaticResource Locator}, Path=MainViewModel.SelectedFolder, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"/>

And in xaml live tree I have loaded grids (DataTemplates for ItemTemplate) in ListView

enter image description here

How can I get each Grid from ListView in code behind?

foreach (var item in this.FoldersListView.Items)
    var recievedItem = this.FoldersListView.ContainerFromItem(item) as ListViewItem;

Solution – 1

How can I get each Grid from ListView in code behind?

You will need to use the VisualTreeHelper Class to achieve this.

  1. Get each ListViewItem by calling ContainerFromItem or ContainerFromIndex of the ListView

  2. Create a recursive function to find DependencyObjects that are grid in side each ListViewItem.

Without knowing your real scenario about the item style, I just made a simple demo to show how it works. You could refer to the following code and change it a little bit for your scenario.


        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    <Button Content="Click" Click="Button_Click"/>
    <ListView Grid.Row="1" x:Name="MyListView">
            <DataTemplate x:DataType="x:String">
                <Grid >
                    <TextBlock Text="{x:Bind}"/>


 public List<string> sourceList { get; set; }
    public MainPage()
        sourceList = new List<string>();
        MyListView.ItemsSource = sourceList;

    private void Button_Click(object sender, RoutedEventArgs e)
        foreach (var stringItem in sourceList) 
            // get every listview item first
            ListViewItem item  = MyListView.ContainerFromItem(stringItem) as ListViewItem;
            // the DependencyObject is the Grid that you want to get
            DependencyObject grid = FindChild(item);

    // a recursive function to find DependencyObjects that are grid
    public DependencyObject FindChild(DependencyObject parant) 
        int count = VisualTreeHelper.GetChildrenCount(parant);

        for (int i = 0; i < count; i++)
            var MyChild = VisualTreeHelper.GetChild(parant, i);
            if (MyChild is Grid)
                // My datatemplate is simple, so I could just get the TextBlock. 
                //If you are using a different datatemplate, you will need to modify the following code to do want you want.
                Grid myGrid = (Grid)MyChild;
                TextBlock textBlock = (TextBlock)myGrid.Children.FirstOrDefault();

                textBlock.Foreground = new SolidColorBrush(Colors.Red);

                return myGrid;
                var FindResult = FindChild(MyChild);
                return FindResult;
        return null;
Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.