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

后端 未结 4 1908
情书的邮戳
情书的邮戳 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 10:53

    To be able to access templated elements by name and by whatever way you like, create a custom view that will serve You as a cell. And in the cell's code-behind you will be able to access everything by name. Your initial parent list will now look like:

    <ListView HasUnevenRows="true" HeightRequest="{Binding ListHeight}">
     <ListView.ItemTemplate>
          <DataTemplate>
              <user:MyViewCell/>
         </DataTemplate>
    </ListView.ItemTemplate>
    </ListView>
    

    Your brand new cell will look like:

    XAML:

       <?xml version="1.0" encoding="UTF-8"?>
        <ViewCell xmlns="http://xamarin.com/schemas/2014/forms" 
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="YourNameSpace.MyViewCell">
        <StackLayout x:Name="btnStack" Spacing="0">
            <Label x:Name="txtEvenMore"/>
        </StackLayout>
    
        </ViewCell>
    

    Code:

    namespace YourNameSpace
    {
        [XamlCompilation(XamlCompilationOptions.Compile)]
        public partial class MyViewCell
        {
            public MyViewCell()
            {
                InitializeComponent();
    
                //you can init your cells here
                InitCell(); //this is just for demo
            }
    
            public void InitCell()
            {
                //i can access my stack:
                btnStack.BackgroundColor = Color.Red;
                //and even more
                txtEvenMore.Text = "By name? Yes! :)";
            }
    
            //Now not for demo but in the real world:
            //We can set content according to your data from ItemsSource
            //This will act when you set your ListView ItemsSource to something valid
            protected override void OnBindingContextChanged()
            {
                SetupCell();
                base.OnBindingContextChanged();
            }
    
            public void SetupCell()
            {
                //use data from ItemsSource
                var item = BindingContext as YourItemClass;
                if (item == null) return;
    
                txtEvenMore.Text = item.SomeTextProperty;
                //etc.. :)
            }
    
       }
     }
    

    Good luck! :)

    0 讨论(0)
  • 2021-01-24 11:02

    Because it is a ListView, there may be multiple StackLayouts. So what you need to do is give the ListView an x:Name property, and then you can access the items in the ListView using it's ItemSource property. Like so:

    <ListView HasUnevenRows="true" HeightRequest="{Binding ListHeight}" x:name="myListView">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                   <StackLayout>
                       <StackLayout />
                   </StackLayout>
                </ViewCell>
           </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    

    Then in your code-behind, simply access the items through myListView.ItemsSource.

    If you really need to manipulate an individual StackLayout in the list view, then you should think about setting it up properly using MVVM pattern.

    0 讨论(0)
  • 2021-01-24 11:10

    When you define x:Name in Xaml, the relevant variable is defined automatically. So you can just use it as it was defined. Something like:

    // StackLayout btnStack; // no need - it is defined already
    var cnt = btnStack.Children.Count; // should be ok
    

    Probably your problem is that in the line:

     <StackLayout x:Name="btnStack/>
    

    you forgot the closing quote. It should be:

    <StackLayout x:Name="btnStack" />
    
    0 讨论(0)
  • 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.

      <StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">  
     <ListView x:Name="PersonListView" HasUnevenRows="True" SeparatorVisibility="Default">  
       <ListView.ItemTemplate>  
         <DataTemplate>  
           <ViewCell>  
             <StackLayout Orientation="Vertical" Padding="10" Margin="10">    
               <Label Text="{Binding name}" />  
               <Label Text="{Binding phone}" />  
               <Label Text="{Binding age}" /> 
               <Button Text="Click to See" Clicked="DisplayData"/>  
             </StackLayout>  
           </ViewCell>  
         </DataTemplate>  
       </ListView.ItemTemplate>  
     </ListView>  
    

     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

    0 讨论(0)
提交回复
热议问题