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! :)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…