如何使用x:codebehind中listView中的项目名称?

时间:2018-02-04 12:01:43

标签: listview xamarin xamarin.forms

有没有办法使用x:xamarin.forms中代码后面的listview中的stacklayout的名称?

 <ListView HasUnevenRows="true" HeightRequist="{Binding ListHeight}">
 <ListView.ItemTemplate>
      <DataTemplate>
          <ViewCell>
             <StackLayout>
                 <StackLayout x:Name="btnStack/>
             </StackLayout>
          </ViewCell>
     </DataTemplate>
</ListView.ItemTemplate>
</ListView>

如何在代码隐藏时使用btnStack

4 个答案:

答案 0 :(得分:1)

此链接说明了listview如何访问元素 ListView每个视图控件仅创建一个实例。 我们无法直接在ListView中访问视图控件。可以间接访问。

  <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

答案 1 :(得分:0)

在Xaml中定义x:Name时,会自动定义相关变量。 因此,您可以按照定义使用它。类似的东西:

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

可能你的问题在于:

 <StackLayout x:Name="btnStack/>
你忘记了结束语。它应该是:

<StackLayout x:Name="btnStack" />

答案 2 :(得分:0)

为了能够按名称和您喜欢的方式访问模板化元素,请创建一个自定义视图,将您作为单元格提供。在单元格的代码隐藏中,您将能够按名称访问所有内容。您的初始父列表现在看起来像:

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

您的全新细胞将如下所示:

<强> 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>

<强>代码

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.. :)
        }

   }
 }
祝你好运! :)

答案 3 :(得分:-1)

因为它是ListView,所以可能有多个StackLayouts。因此,您需要做的是为ListView提供一个x:Name属性,然后您可以使用它的ItemSource属性访问ListView中的项目。像这样:

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

然后在您的代码隐藏中,只需通过myListView.ItemsSource访问这些项目。

如果您确实需要在列表视图中操作单个StackLayout,那么您应该考虑使用MVVM模式正确设置它。