How to use x:Name of items in listView at codebehind?

后端 未结 4 1948
情书的邮戳
情书的邮戳 2021-01-24 10:27

Is there any way to use x:Name of the stacklayout in listview at code behind in xamarin.forms?

 

        
4条回答
  •  自闭症患者
    2021-01-24 11:12

    This link explain how access elements withn listview ListView creates only one instance of each View Controls. We cannot access View Controls inside ListView directly. can be accessed indirectly.

        
       
         
           
             
                 
               

     async void DisplayData(object Sender, EventArgs e)  
     {  
       // access buttonclickhandler  
       var buttonClickHandler = (Button)Sender;  
    
       // access Parent Layout for Button  
       StackLayout ParentStackLayout = (StackLayout)buttonClickHandler.Parent;  
    
       // access first Label "name"  
       Label nameLabel = (Label)ParentStackLayout.Children[0];  
    
       // access second Label "phone"  
       Label phoneLabel = (Label)ParentStackLayout.Children[1];  
    
       // access third Label "age"  
       Label ageLabel = (Label)ParentStackLayout.Children[2];  
    
       string name = nameLabel.Text;  
       string phone = phoneLabel.Text;  
       string age = ageLabel.Text;  
       await DisplayAlert("Person Details", "Name is : " + name + ".\n Phone Number is: " + phone + "\n Age is : " + age, "OK");  
     }  
    

    https://www.xamarinpaul.com/2018/11/how-to-access-view-controls-inside-Xamarin-forms-ListView.html

提交回复
热议问题