Reputation: 3756
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:
Why row not display?
How can display listview from a datatable?
Upvotes: 0
Views: 1659
Reputation: 2995
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 */));
Upvotes: 1
Reputation: 1911
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>
Upvotes: 3
Reputation: 673
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.
Upvotes: 0