0

This is design xaml: it include a listview display list image, title, subtilte.

             <ListView  x:Name="listView">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout BackgroundColor="#eee"
                        Orientation="Vertical">
                                <StackLayout Orientation="Horizontal">
                                    <Image Source="{Binding image}" />
                                    <Label Text="{Binding title}"
                                TextColor="#f35e20" />
                                    <Label Text="{Binding subtitle}"
                                HorizontalOptions="EndAndExpand"
                                TextColor="#503026" />
                                </StackLayout>
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

I add process load data to listview:

public ListAccount ()
        {
            InitializeComponent ();
            var dt = new System.Data.DataTable();
            dt.Columns.Add("image");
            dt.Columns.Add("title");
            dt.Columns.Add("subtitle");
            var dr = dt.NewRow();
            dr["image"] = "a.jpg";
            dr["title"] = "title";
            dr["subtitle"] = "subtitle1";
            dt.Rows.Add(dr);
            listView.ItemsSource = dt.DefaultView ;
        }

Result:

enter image description here

Why row not display?

How can display listview from a datatable?

3 Answers 3

3

Instead of Datatable try Model as below. It will display more beatiful

public ListAccount()
    {
        InitializeComponent();
        List<ItemModel> itemModels = new List<ItemModel>();
        itemModels.Add(new ItemModel()
        {
            Image = "Image1",
            SubTitle = "Subbtitle1",
            Title = "Title1"
        });
        itemModels.Add(new ItemModel()
        {
            Image = "Image2",
            SubTitle = "Subbtitle2",
            Title = "Title2"
        });
        listView.ItemsSource = itemModels;
    }

    public class ItemModel
    {
        public string Title { get; set; }
        public string Image { get; set; }
        public string SubTitle { get; set; }
    }

XAML Code:

 <ListView  x:Name="listView">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout BackgroundColor="#eee"
                    Orientation="Vertical">
                            <StackLayout Orientation="Horizontal">
                                <Image Source="{Binding Image}" />
                                <Label Text="{Binding Title}"
                            TextColor="#f35e20" />
                                <Label Text="{Binding SubTitle}"
                            HorizontalOptions="EndAndExpand"
                            TextColor="#503026" />
                            </StackLayout>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
Sign up to request clarification or add additional context in comments.

Comments

1

Declare a type:

public class MyType
{
    public string title { get; set; }
    // more properties

    public MyType(string title, /* more arguments */)
    {
        this.title = title;
        /* set more properties from arguments */ 
    }
}

Set ItemsSource:

listView.ItemsSource = 
    dt.Select().ToList().Select(r =>
        new MyType(r["title"] as string, /* more arguments */));

Comments

0

Any platform-specific code is not supported in a PCL. As with any platform-specific view you will need to implement a custom renderer. This blog post describes how to do that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.