Home > database >  Getting UI Element from list view item template
Getting UI Element from list view item template

Time:12-01

I have ListView in my app that have style.

<ListView x:Name="FoldersListView"
          RightTapped="OnFoldersListViewRightTapped"                        
          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;
}

CodePudding user response:

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.

MainPage.Xaml

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

MainPage.Cs

 public List<string> sourceList { get; set; }
    public MainPage()
    {
        this.InitializeComponent();
        sourceList = new List<string>();
        sourceList.Add("1");
        sourceList.Add("2");
        sourceList.Add("3");
        sourceList.Add("4");
        sourceList.Add("5");
        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;
            }
            else
            {
                var FindResult = FindChild(MyChild);
                return FindResult;
            }
        }
        return null;
    }
  • Related