sah*_*thi 2 listview xamarin xamarin.forms
有什么方法可以在 xamarin.forms 的代码后面的列表视图中使用 x:Name of the stacklayout?
<ListView HasUnevenRows="true" HeightRequist="{Binding ListHeight}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<StackLayout x:Name="btnStack/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Run Code Online (Sandbox Code Playgroud)
如何btnStack在代码隐藏处使用?
为了能够通过名称和任何您喜欢的方式访问模板化元素,请创建一个自定义视图,将您作为一个单元。在单元格的代码隐藏中,您将能够按名称访问所有内容。您的初始父列表现在将如下所示:
<ListView HasUnevenRows="true" HeightRequest="{Binding ListHeight}">
<ListView.ItemTemplate>
<DataTemplate>
<user:MyViewCell/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Run Code Online (Sandbox Code Playgroud)
您全新的单元格将如下所示:
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>
Run Code Online (Sandbox Code Playgroud)
代码:
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.. :)
}
}
}
Run Code Online (Sandbox Code Playgroud)
祝你好运!:)
| 归档时间: |
|
| 查看次数: |
6506 次 |
| 最近记录: |