【问题标题】:Multiple Custom Cell's in a ListView (Cross Platform)ListView 中的多个自定义单元格(跨平台)
【发布时间】:2015-02-24 05:37:52
【问题描述】:

目前使用 ListView 的我只发现您可以为单元格创建模板,这使得每个单元格看起来完全相同。列表视图中不能有多个自定义单元格。有一些变通方法,例如根据内容将内容隐藏在单元格中,但这似乎很老套。

我想在 tableview 上使用 listview 的原因是因为我们计划进行插入、删除、动态显示某些单元格,并且 listview 可以绑定到数据源。

【问题讨论】:

  • 您需要的是DataTemplateSelector。您可以轻松地自己构建一个,有很多 Windows Phone 8 版本的文档,您可以将其用作实现的基本代码。

标签: xamarin xamarin.forms


【解决方案1】:

创建您自己的 ViewCell 覆盖绑定上下文更改方法。当绑定更改时,将 ViewCell 的视图设置为与视图模型类型匹配的视图,并设置单元格的高度。下面是一个快速示例,可以让您了解如何完成它。

public class DataTemplateCell1 : ViewCell
{
    protected override void OnBindingContextChanged()
    {
        var vm1 = this.BindingContext as ViewModel1;
        if (vm1 != null)
        {
            this.View = new View1() { HeightRequest = 40 };
            this.Height = this.View.HeightRequest;
            return;
        }

        var vm2 = this.BindingContext as ViewModel2;
        if (vm2 != null)
        {
            this.View = new View2() { HeightRequest = 80 };
            this.Height = this.View.HeightRequest;
            return;
        }

        base.OnBindingContextChanged();
    }
}

【讨论】:

    猜你喜欢
    • 2018-01-31
    • 2019-01-22
    • 2013-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-19
    • 2011-03-11
    • 2020-11-05
    相关资源
    最近更新 更多