自定义单元格

在cs代码中完成

自定义单元格

public class EmployeeCell : ViewCell
{
    public EmployeeCell()
    {
        var image = new Image
        {
            HorizontalOptions = LayoutOptions.Start
        };
        image.SetBinding(Image.SourceProperty, new Binding("ImageUri"));
        //设置宽高为40
        image.WidthRequest = image.HeightRequest = 40;

        var nameLayout = CreateNameLayout();
        var viewLayout = new StackLayout()
        {
            Orientation = StackOrientation.Horizontal,
            //加入图片、名称、推特
            Children = { image, nameLayout }
        };
        //把布局赋值给View
        View = viewLayout;
    }

    static StackLayout CreateNameLayout()
    {
        //新增Label
        var nameLabel = new Label
        {
            HorizontalOptions = LayoutOptions.FillAndExpand
        };
        //绑定Employee的DisplayName属性
        nameLabel.SetBinding(Label.TextProperty, "DisplayName");

        var twitterLabel = new Label
        {
            HorizontalOptions = LayoutOptions.FillAndExpand
        };
        twitterLabel.SetBinding(Label.TextProperty, "Twitter");

        var nameLayout = new StackLayout()
        {
            HorizontalOptions = LayoutOptions.StartAndExpand,
            //设置为从上到下排列
            Orientation = StackOrientation.Vertical,
            //将两个Label依次添加到Children中
            Children = { nameLabel, twitterLabel }
        };
        return nameLayout;
    }
}

后台

List<Employee> EmployeeList = new List<Employee>();
EmployeeList.Add(new Employee()
{
    DisplayName = "Jack",
    Twitter ="@fake4",
    ImageUri= "http://v1.qzone.cc/avatar/201406/24/21/03/53a977066f053731.jpg!200x200.jpg"
});
EmployeeList.Add(new Employee()
{
    DisplayName = "Tom",
    Twitter = "@mml1",
    ImageUri= "http://diy.qqjay.com/u2/2014/0628/da687c0fb5b3bb8cd31dec7d8865aea8.jpg"
});
EmployeeList.Add(new Employee()
{
    DisplayName = "Tony",
    Twitter = "@wood564",
    ImageUri= "http://v1.qzone.cc/avatar/201406/24/21/03/53a977066f053731.jpg!200x200.jpg"
});
var listView = new ListView
{
    RowHeight = 80
};
listView.ItemsSource = EmployeeList;
//注意:此时指定模板为写好的EmployeeCell
listView.ItemTemplate = new DataTemplate(typeof(EmployeeCell));
Content = new StackLayout
{
    VerticalOptions = LayoutOptions.FillAndExpand,
    Children = { listView }
};

Employee类

public class Employee
{
    public string DisplayName { get; set; }
    public string Twitter { get; set; }
    public string ImageUri { get; set; }
}

效果

1

在xaml中完成

后台赋值

List<Employee> Employees = new List<Employee>();
//添加数据
BindingContext = Employees;

前端绑定对应属性名
BindingContext已经赋值,ItemsSource直接绑定下来即可

<ListView x:Name="listView" ItemsSource="{Binding }">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <ViewCell.View>
                    <StackLayout Orientation="Horizontal">
                        <Image Source="{Binding ImageUri}" WidthRequest="40" HeightRequest="40" />
                        <StackLayout Orientation="Vertical" HorizontalOptions="StartAndExpand">
                            <Label Text="{Binding DisplayName}" HorizontalOptions="FillAndExpand" />
                            <Label Text="{Binding Twitter}"/>
                        </StackLayout>
                    </StackLayout>
                </ViewCell.View>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

注意:Employee实不实现INotifyPropertyChanged接口,均可以展示,只是看你做不做双向绑定

示例代码

https://github.com/zLulus/NotePractice/tree/dev3/Xamarin.Forms/XamarinDemo/XamarinDemo/XamarinDemo/CustomizingCell

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 记录一个菜鸟的iOS学习之旅,如能帮助正在学习的你,亦枫不胜荣幸;如路过的大神如指教几句,亦枫感激涕淋! iOS系...
    亦枫阅读 1,870评论 4 9
  • 记录一个菜鸟的iOS学习之旅,如能帮助正在学习的你,亦枫不胜荣幸;如路过的大神如指教几句,亦枫感激涕淋! 在亦枫的...
    亦枫阅读 1,557评论 2 5
  • 记录一个菜鸟的iOS学习之旅,如能帮助正在学习的你,亦枫不胜荣幸;如路过的大神如指教几句,亦枫感激涕淋! 在前面两...
    亦枫阅读 1,575评论 0 5
  • 1、你差哪里都差没得救 刚上一年级的小明期末考试全班倒数第一,小明妈妈爸爸坐在小明左右,一个拿着考试卷问,你给妈妈...
    雅米饭阅读 220评论 0 0
  • 处暑侵晨,时雷霆大作,暴雨将至。路师弟燥热难耐辗转未眠。在床侧反复滚动,楚师兄几欲入眠奈何动静甚大,孰不可忍,抓过...
    沉肃阅读 271评论 0 1