gpt4 book ai didi

listview - 如何使用 x :Name of items in listView at codebehind?

转载 作者:行者123 更新时间:2023-12-05 01:43:42 24 4
gpt4 key购买 nike

有什么方法可以在 xamarin.forms 的代码后面的 ListView 中使用 x:Stacklayout 的名称?

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

如何在代码隐藏中使用btnStack

最佳答案

为了能够按名称和您喜欢的任何方式访问模板化元素,请创建一个自定义 View ,将您用作单元格。在单元格的代码隐藏中,您将能够通过名称访问所有内容。您的初始 parent 列表现在如下所示:

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

}
}

祝你好运! :)

关于listview - 如何使用 x :Name of items in listView at codebehind?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48607818/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com