Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
943 views
in Technique[技术] by (71.8m points)

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

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

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

How to use btnStack at codebehind?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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! :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...