How to dynamically generate GridLayout to contain Button and data - xamarin.forms

Have no clue as to how to handle this scenario dynamically after(2)
1) the app call the REST Json web service
2) Deserialize the Json response
3) Bind the Data to ListView
below is the working example.
Problem : I need to add a button to each row of record. Example , How to add a button next to Johnny Depp?? Add EventHandler for this button?
This means I need to dynamically generate the XAML? How to bind ?
here to code to handle the Returned Json Result:
-- Model:
public class Phone
{
public string mobile { get; set; }
public string home { get; set; }
public string office { get; set; }
}
public class Contact
{
public string id { get; set; }
public string name { get; set; }
public string email { get; set; }
public string address { get; set; }
public string gender { get; set; }
public Phone phone { get; set; }
}
public class ContactList
{
public List<Contact> contacts { get; set; }
}
---- Call REST
var client = new System.Net.Http.HttpClient();
var response = await client.GetAsync("http://Rest api");
string contactsJson = response.Content.ReadAsStringAsync().Result;
ContactList ObjContactList = new ContactList();
if (contactsJson != "")
{
ObjContactList = JsonConvert.DeserializeObject<ContactList>(contactsJson);
}
listviewConacts.ItemsSource = ObjContactList.contacts;
------XAML:
<Grid>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Label Grid.Row="0" Margin="10" Text="Display Json Data" FontSize="25" />
<ListView x:Name="listviewConacts" Grid.Row="1" HorizontalOptions="FillAndExpand" HasUnevenRows="True" ItemSelected="listviewContacts_ItemSelected">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid HorizontalOptions="FillAndExpand" Padding="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label Text="{Binding name}" HorizontalOptions="StartAndExpand" Grid.Row="0" TextColor="Blue" FontAttributes="Bold"/>
<Label Text="{Binding email}" HorizontalOptions="StartAndExpand" Grid.Row="1" TextColor="Orange" FontAttributes="Bold"/>
<Label Text="{Binding phone.mobile}" HorizontalOptions="StartAndExpand" Grid.Row="2" TextColor="Gray" FontAttributes="Bold"/>
<BoxView HeightRequest="2" Margin="0,10,10,0" BackgroundColor="Gray" Grid.Row="3" HorizontalOptions="FillAndExpand" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
<ActivityIndicator x:Name="ProgressLoader" IsRunning="True"/>
</Grid>
Thanks

You created custom cell already, add button to it with Command and CommandParameter. Inside your viewcell grid add
<Button Text="Toggle" Command="{Binding Source={x:Reference YourPage}, Path=BindingContext.TapCommand}" CommandParameter="{Binding}"/>
Then in your model
public Command TapCommand
{
get
{
return new Command( (obj) =>
{
//obj here will be your data record
});
}
}

Related

How to dynamically set element visibility of items in a list view in xamarin forms

I am trying to create a page in Xamarin forms which mimics a table style in HTML. I want the header and data to be able to remove or displayed based on the condition. My problem is I could not figure out a way to access certain property inside the ViewCell of List view.
My XAML code is like this
<Grid x:DataType="models:TransactionHistory" >
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Label Grid.Row="0"
Grid.Column="0"
Text="Tran Number"
x:Name="LabelTranNumber"
IsVisible="{Binding ShowTransactionNumberColumn}"/>
<Label Grid.Row="0"
Grid.Column="1"
Text="Description"
x:Name="LabelDescription"
IsVisible="{Binding ShowDescriptionColumn}"/>
<Label
Grid.Row="0"
Grid.Column="2"
Text="Quantity"
x:Name="LabelQuantity"
IsVisible="{Binding ShowQuantityColumn}"
/>
<Label
Grid.Row="0"
Grid.Column="3"
Text="Date"
x:Name="LabelDate"
IsVisible="{Binding ShowDateRaisedColumn}"/>
<Label
Grid.Row="0"
Grid.Column="4"
Text="Extension"
x:Name="LabelExtension"
IsVisible="{Binding ShowExtensionColumn}"/>
<ListView
Grid.Column="0"
Grid.Row="1"
Grid.ColumnSpan="{Binding NumberOfVisibleColumns}"
x:Name="ListViewTransactionHistory">
<ListView.ItemTemplate >
<DataTemplate x:DataType="models:History" >
<ViewCell>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Label
Grid.Column="0"
Text="{Binding TransactionNumber}"
IsVisible="{Binding??? }"/> --- How do I access ShowTransactionNumber field here?
<Label Grid.Row="0"
Grid.Column="1"
Text="{Binding Description}" />
<Label
Grid.Column="2"
Text="{Binding Quantity}" />
<Label
Grid.Column="3"
Text="{Binding DateRaised}" />
<Label
Grid.Column="4"
Text="{Binding FormattedExtension}" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
My Model is like this
public class TransactionHistory
{
public bool ShowTransactionNumberColumn { get; set; }
public bool ShowDescriptionColumn { get; set; }
public bool ShowQuantityColumn { get; set; }
public bool ShowDateRaisedColumn { get; set; }
public bool ShowExtensionColumn { get; set; }
public int NumberOfVisibleColumns {
get
{
int numberOfVisibleColumns = 0;
if (ShowTransactionNumberColumn) numberOfVisibleColumns++;
if (ShowDescriptionColumn) numberOfVisibleColumns++;
if (ShowQuantityColumn) numberOfVisibleColumns++;
if (ShowDateRaisedColumn) numberOfVisibleColumns++;
if (ShowExtensionColumn) numberOfVisibleColumns++;
return numberOfVisibleColumns;
}
}
public List<History> TransactionHistories { get; set; }
}
My History class is like this
public class History
{
public string TransactionNumber { get; set; }
public string Description { get; set; }
public decimal Quantity { get; set; }
public string DateRaised { get; set; }
public decimal Extension { get; set; }
public string FormattedExtension { get; set; }
}
My question is how do I bind the Transaction History model to view so that my list view is initialised with TransactionHistories. And How can I access the boolean variables inside the cell to toggle visibility of an element inside a cell?
In the code behind I can set the source of list view like this. But I won't be able to access the booleans.
ListViewTransactionHistory.ItemsSource = transactionHistory.TransactionHistories;
I think I am doing something wrong. Any idea will be helpful. Many Thanks :)
Edit:
On the code behind OnAppearing method I am doing this
protected override void OnAppearing()
{
TransactionHistoryStore transactionHistoryStore = new TransactionHistoryStore();
TransactionHistory transactionHistory = transactionHistoryStore.GetTransactions();
if (transactionHistory == null) return;
LabelTranNumber.IsVisible = transactionHistory.ShowTransactionNumberColumn;
LabelDescription.IsVisible = transactionHistory.ShowDescriptionColumn;
LabelDate.IsVisible = transactionHistory.ShowDateRaisedColumn;
LabelExtension.IsVisible = transactionHistory.ShowExtensionColumn;
// NumberOfVisibleColumns = numberOfVisibleColumns;
ListViewTransactionHistory.ItemsSource = transactionHistory.TransactionHistories;
}
So instead of setting IsVisible property manually. How can I bind the transactionHistory variable.
The transactionHistoryVariable is like this
public TransactionHistory GetTransactions()
{
TransactionHistory transaction = new TransactionHistory
{
ShowDescriptionColumn = true,
ShowQuantityColumn = true,
ShowDateRaisedColumn = true,
ShowExtensionColumn = true,
ShowTransactionNumberColumn = true,
TransactionHistories = new List<History>
{
new History
{
DateRaised = DateTime.Now.ToShortDateString(),
Description = "SMITH CHIPS ",
Extension = 2.25M,
FormattedExtension = 2.25.ToString("C"),
Quantity = 2,
TransactionNumber = "1234"
},
new History
{
DateRaised = DateTime.Now.ToShortDateString(),
Description = "SMITH CHIPS ",
Extension = 2.25M,
FormattedExtension = 2.25.ToString("C"),
Quantity = 2,
TransactionNumber = "1234"
},
new History
{
DateRaised = DateTime.Now.ToShortDateString(),
Description = "SMITH CHIPS ",
Extension = 2.25M,
FormattedExtension = 2.25.ToString("C"),
Quantity = 2,
TransactionNumber = "1234"
}
}
};
return transaction;
}
If I understood your question.
In your XAML set up the ListView ItemsSource={Binding TransactionHistories} (Looks like you already set the binding of the page)
In order you access the page binding, you have to use this small
binding trick:
Set up an x:Name for your parent ContentPage
In the element you want to hide, add this: {Binding Source={x:Reference NameOfPage}, Path=BindingContext.NameOfProperty}
More about it Here

Unable to build my app after connecting it to sqlite

Ok so when i run my app i just get a : System.AggregateException which i tried to spot with the debuguer
and it brings me to this function in the AgendaDatabase.cs file :
public AgendaDatabase(string dbPath)
{
database = new SQLiteAsyncConnection(dbPath);
database.CreateTableAsync<Agenda>().Wait();
}
Before targetting the System.AggregateException with the debugger i also had a SystemAggregateException for this function in AcceuilPage.xaml.cs:
protected override async void OnAppearing()
{
base.OnAppearing();
AgendaCollection.ItemsSource = await App.Database.GetAgendasAsync();
}
and when i execute one more it says : unhandled exception : SQLite.SQLiteException: no such table: Agenda which is weird cause the code is supposed to create it if it doesn't exist.
This is the tutorial i am following : https://learn.microsoft.com/en-us/xamarin/get-started/quickstarts/database?pivots=windows
Thanks for your help.
I've done the steps mutiple time, even listened to similar tutorial with no solution on how to fix it : heres the code :
AgendaDatabase.cs (in Database folder)
using System;
using System.Collections.Generic;
using System.Text;
using SQLite;
using Calculette.Models;
using System.Threading.Tasks;
namespace Calculette.Database
{
public class AgendaDatabase
{
readonly SQLiteAsyncConnection database;
public AgendaDatabase(string dbPath)
{
database = new SQLiteAsyncConnection(dbPath);
database.CreateTableAsync<Agenda>().Wait();
}
public Task<List<Agenda>> GetAgendasAsync()
{
return database.Table<Agenda>().ToListAsync();
}
public Task<Agenda> GetAgendaAsync(int id)
{
return database.Table<Agenda>()
.Where(i => i.ID == id)
.FirstOrDefaultAsync();
}
public Task<int> SaveAgendaAsync(Agenda agenda)
{
if (agenda.ID != 0)
{
return database.UpdateAsync(agenda);
}
else
{
return database.InsertAsync(agenda);
}
}
public Task<int> DeleteAgendaAsync(Agenda agenda)
{
return database.DeleteAsync(agenda);
}
}
}
Agenda.cs in the Models folder
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using SQLite;
using Calculette.Database;
namespace Calculette.Models
{
public class Agenda
{
[PrimaryKey, AutoIncrement]
public int ID { get; set; }
public string Topic { get; set; }
public string Duration { get; set; }
public DateTime Date { get; set; }
public ObservableCollection<Speaker> Speakers { get; set; }
public string Color { get; set; }
public string Name { get; set; }
public string Time { get; set; }
}
}
AcceuilPage.xaml in the Views folder
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Calculette"
xmlns:pv="clr-namespace:Xamarin.Forms.PancakeView;assembly=Xamarin.Forms.PancakeView"
x:Class="Calculette.MainPage"
BarBackgroundColor = "White"
BarTextColor="#008A00">
<ContentPage Icon="icontache.png" BackgroundColor="#F6F8F9">
<ContentPage.Content>
<!-- ScrollView nous permet d'avoir une page scrollable-->
<ScrollView Orientation="Vertical">
<CollectionView Grid.Row="2" Margin="25" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"
SelectionMode="None" x:Name="AgendaCollection">
<CollectionView.Header>
<StackLayout Orientation="Horizontal" Spacing="220">
<Label Text="Agenda" TextColor="Black" FontSize="18"/>
<ImageButton Source="iconplus.png" HeightRequest="30" WidthRequest="30" Clicked="GoToNewFormPage"></ImageButton>
</StackLayout>
</CollectionView.Header>
<CollectionView.ItemsLayout>
<LinearItemsLayout Orientation="Vertical" ItemSpacing="20"/>
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate >
<DataTemplate>
<pv:PancakeView HasShadow="True" BackgroundColor="White" VerticalOptions="StartAndExpand "
HorizontalOptions="FillAndExpand" >
<Grid VerticalOptions="StartAndExpand" HorizontalOptions="FillAndExpand">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<BoxView BackgroundColor="{Binding Color}" WidthRequest="3" HorizontalOptions="Start"
VerticalOptions="FillAndExpand"/>
<Expander Grid.Column="1">
<Expander.Header>
<Grid HorizontalOptions="FillAndExpand">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="3.5*"/>
</Grid.ColumnDefinitions>
<StackLayout HorizontalOptions="Center" VerticalOptions="Center">
<Label Text="{Binding Date, StringFormat='{0:dd}'}" TextColor="#008A00" FontSize="27"
HorizontalOptions="Center"/>
<Label Text="{Binding Date, StringFormat='{0:MMMM}'}" TextColor="Black" FontSize="10"
HorizontalOptions="Center" Margin="0,-10,0,0" FontAttributes="Bold"/>
<ImageButton Source="iconplus.png" HorizontalOptions="Center" HeightRequest="30" WidthRequest="30" Clicked="GoToFormPage"></ImageButton>
</StackLayout>
<BoxView Grid.Column="1" BackgroundColor="#F2F4F8" WidthRequest="1" HorizontalOptions="Start"
VerticalOptions="FillAndExpand"/>
<StackLayout Grid.Column="2" HorizontalOptions="Start" VerticalOptions="Center" Margin="20">
<Label Text="{Binding Topic}" TextColor="#008A00" FontSize="15" FontAttributes="Bold"/>
<Label Text="{Binding Duration}" TextColor="#2F3246" FontSize="12" Margin="0,-10,0,0"/>
</StackLayout>
</Grid>
</Expander.Header>
<Grid HorizontalOptions="FillAndExpand">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="3.5*"/>
</Grid.ColumnDefinitions>
<BoxView Grid.Column="1" BackgroundColor="#F2F4F8" WidthRequest="1" HorizontalOptions="Start"
VerticalOptions="FillAndExpand"/>
<StackLayout Grid.Column="2" Spacing="10">
<Label Text="Tâches" TextColor="Black" FontSize="15" Margin="20,0"/>
<StackLayout BindableLayout.ItemsSource="{Binding Speakers}" HorizontalOptions="Start" VerticalOptions="Center" Margin="20,0,0,20">
<BindableLayout.ItemTemplate>
<DataTemplate>
<Label TextColor="#2F3246" FontSize="12">
<Label.FormattedText>
<FormattedString>
<FormattedString.Spans>
<Span Text="{Binding Time}"/>
<Span Text=" - "/>
<Span Text="{Binding Name}" FontAttributes="Bold"/>
</FormattedString.Spans>
</FormattedString>
</Label.FormattedText>
</Label>
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
</StackLayout>
</Grid>
</Expander>
</Grid>
</pv:PancakeView>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</ScrollView>
</ContentPage.Content>
</ContentPage>
AcceuilPage.xaml.cs
using Calculette.ViewModel;
using Calculette.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.PancakeView;
namespace Calculette
{
public partial class MainPage : TabbedPage
{
public MainPage()
{
InitializeComponent();
this.BindingContext = this;
}
protected async void GoToFormPage(object sender, EventArgs e)
{
await Navigation.PushAsync(new Views.AgendaItemDetailPage());
}
protected async void GoToNewFormPage(object sender, EventArgs e)
{
await Navigation.PushAsync(new Views.NewFormPage());
}
protected override async void OnAppearing()
{
base.OnAppearing();
AgendaCollection.ItemsSource = await App.Database.GetAgendasAsync();
}
}
}
First of all, you must specify database model, because sqlite is not able to create table from your model.
namespace Calculette.Models
{
[Table("Agenda")]
public class Agenda
{
[PrimaryKey, AutoIncrement, Column("ID")]
public int ID { get; set; }
[Column("Topic")]
public string Topic { get; set; }
[Column("Duration")]
public string Duration { get; set; }
//public DateTime Date { get; set; }
//public ObservableCollection<Speaker> Speakers { get; set; }
[Column("Color")]
public string Color { get; set; }
[Column("Name")]
public string Name { get; set; }
[Column("Time")]
public string Time { get; set; }
}
}
Be aware that sqlite does not support
public DateTime Date { get; set; }
public ObservableCollection<Speaker> Speakers { get; set; }

xamarin.forms listview Items only update when scrolling even after iNotifyPropertychanged implementation

I have a xamarin.forms app which have a listview showing item fetched from firebase. The issue I am facing is well asked question in SO ie; one of my item in listview (an item named location ) only updates when we scroll the listview. I tried to implement the iNotifyPropertychanged but still I am facing the same issue. What is the mistake I am doing here? Any help is appriciated.
My Listview Itemsource setting
var person = await firebaseHelper.GetPerson(EmployeeID);// getting data from firebase
LocationData = person.userdata.Where(x => DateTime.Parse(x.DateTime).ToString("yyyy-MM-dd") == StartDate.Date.ToString("yyyy-MM-dd") && x.Longitude != "initial").ToList();
ObservableCollection<UserLocationData> dynamicLocation = new ObservableCollection<UserLocationData>(LocationData);
TrackingListView.ItemsSource = dynamicLocation;
if (dynamicLocation.Count != 0)
{
foreach (UserLocationData Item in dynamicLocation)
{
Item.DateTime = DateTime.Parse(Item.DateTime).ToString("MMMM dd,yyyy hh:mm tt");
if (!string.IsNullOrEmpty(Item.Latitude) && !string.IsNullOrEmpty(Item.Longitude))
{
var Locations = await Geocoding.GetPlacemarksAsync(Convert.ToDouble(Item.Latitude), Convert.ToDouble(Item.Longitude));
var placemark = Locations?.FirstOrDefault();
if (placemark.Thoroughfare.ToString() != null)
{
var geocodeAddress =
placemark.Thoroughfare + '\n' +
placemark.SubAdminArea + '\n';
Item.Location = geocodeAddress.ToString();
}
else
{
Item.Location = Item.Latitude + "," + Item.Longitude;
}
}
}
My Datamodel
public class Person : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public string Name { get; set; }
public string PersonId { get; set; }
public string Address { get; set; }
public string PhoneNumber { get; set; }
public string EmailID { get; set; }
public string Password { get; set; }
public bool Status { get; set; }
public bool IsAdmin { get; set; }
public List<UserLocationData> userdata { get; set; }
}
public partial class UserLocationData : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public string Latitude { get; set; }
public string Longitude { get; set; }
public string DateTime { get; set; }
private string _location;
public string Location
{
get
{
return _location;
}
set
{
if (value != null)
{
_location = value;
NotifyPropertyChanged("Selected");
}
}
}
public bool ButtonOn { get; set; }
public bool ButtonOff { get; set; }
}
The Firebase GetPerson portion
public async Task<Person> GetPerson(string personId)
{
var allPersons = await GetAllPersons();
await firebase
.Child("Persons")
.OnceAsync<Person>();
return allPersons.Where(a => a.PersonId == personId).FirstOrDefault();
}
My XAML
<ListView x:Name="TrackingListView" ItemsSource="{Binding} "
HasUnevenRows="True"
IsVisible="False"
CachingStrategy="RecycleElement"
SeparatorVisibility="None"
SelectionMode="None"
BackgroundColor="Transparent"
Margin="10,10,10,10"
HorizontalOptions="FillAndExpand"
>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<Frame
Padding="5"
Margin="0,5,0,5"
Opacity="0.9"
BackgroundColor="White"
BorderColor="LightBlue"
HasShadow="True"
CornerRadius="10"
ClassId="{Binding DateTime}"
HorizontalOptions="FillAndExpand">
<Frame.GestureRecognizers>
<TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"></TapGestureRecognizer>
</Frame.GestureRecognizers>
<Grid Margin="0,10,0,0" >
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<StackLayout HorizontalOptions="FillAndExpand" Grid.Row="0" Grid.Column="0" Orientation="Horizontal">
<Image Source="clock.png" HorizontalOptions="Start" VerticalOptions="Start"
HeightRequest="20" Margin="10,0,5,0"
></Image>
<StackLayout Orientation="Vertical" Margin="0" HorizontalOptions="FillAndExpand">
<Label Text="Location fetched time" FontSize="12" HorizontalOptions="StartAndExpand" VerticalOptions="Center"
TextColor="Blue"
></Label>
<Label Text="{Binding DateTime}" HorizontalOptions="StartAndExpand" VerticalOptions="Center"
TextColor="Black" FontSize="Small"
></Label>
</StackLayout>
</StackLayout>
<StackLayout Grid.Column="1" Grid.Row="0" HorizontalOptions="EndAndExpand" VerticalOptions="FillAndExpand">
<Image Source="googlemap.png" HorizontalOptions="End" VerticalOptions="Center" HeightRequest="30" ></Image>
</StackLayout>
<StackLayout HorizontalOptions="FillAndExpand" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Orientation="Horizontal">
<Image Source="currentlocation.png" HorizontalOptions="Start" VerticalOptions="Start"
HeightRequest="20" Margin="10,0,5,0"
></Image>
<StackLayout Orientation="Vertical" Margin="0" HorizontalOptions="FillAndExpand">
<Label Text="Location " FontSize="12" HorizontalOptions="StartAndExpand" VerticalOptions="Center"
TextColor="Blue"
></Label>
<Label Text="{Binding Location}" HorizontalOptions="StartAndExpand" VerticalOptions="Center"
TextColor="Black" FontSize="Small"
></Label>
</StackLayout>
</StackLayout>
<StackLayout Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" Margin="0" >
<Grid>
<StackLayout Orientation="Horizontal" Margin="0" IsVisible="{Binding ButtonOn}">
<Image Source="start.png" HorizontalOptions="Start"
HeightRequest="20" Margin="10,0,5,0"
VerticalOptions="Start">
</Image>
<Label Text="Location Tracking Started" VerticalOptions="Center" TextColor="Green" FontSize="12" ></Label>
</StackLayout>
<StackLayout Orientation="Horizontal" Margin="0" IsVisible="{Binding ButtonOff}">
<Image Source="stop.png" HorizontalOptions="Start"
HeightRequest="20" Margin="10,0,5,0"
VerticalOptions="Start">
</Image>
<Label Text="Location Tracking Stopped" VerticalOptions="Center" TextColor="Red" FontSize="12" ></Label>
</StackLayout>
</Grid>
</StackLayout>
</Grid>
</Frame>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
NotifyPropertyChanged("Selected"); it should be NotifyPropertyChanged("Location");

How to expand and Collapse ListView in Xamarin Forms

I'm new to Xamarin Forms and I understand that there are many useful controls. I'm looking for a control that can expand to show data in a grid like in the example below.
Update
Model:
public class Phone
{
public string mobile { get; set; }
public string home { get; set; }
public string office { get; set; }
}
public class Contact
{
public string id { get; set; }
public string name { get; set; }
public string email { get; set; }
public string address { get; set; }
public string gender { get; set; }
public Phone phone { get; set; }
}
public class ContactList
{
public List<Contact> contacts { get; set; }
}
XAML:
<Grid>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Label Grid.Row="0" Margin="10" Text="Display Json Data" FontSize="25" />
<ListView x:Name="listviewConacts" Grid.Row="1" HorizontalOptions="FillAndExpand" HasUnevenRows="True" ItemSelected="listviewContacts_ItemSelected">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid HorizontalOptions="FillAndExpand" Padding="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label Text="{Binding name}" HorizontalOptions="StartAndExpand" Grid.Row="0" TextColor="Blue" FontAttributes="Bold"/>
<Label Text="{Binding email}" HorizontalOptions="StartAndExpand" Grid.Row="1" TextColor="Orange" FontAttributes="Bold"/>
<Label Text="{Binding phone.mobile}" HorizontalOptions="StartAndExpand" Grid.Row="2" TextColor="Gray" FontAttributes="Bold"/>
<BoxView HeightRequest="2" Margin="0,10,10,0" BackgroundColor="Gray" Grid.Row="3" HorizontalOptions="FillAndExpand" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
<ActivityIndicator x:Name="ProgressLoader" IsRunning="True"/>
</Grid>
Based on the model and XAML above, how can I achieve an expandable and collapsible ListView such as the one in image above?
In ListView to show the data just take one GridLayout in ViewCell. Take two rows with height auto in GridLayout. In first row show header and button and in second row just add that item relatesd data and bind one isvisible property to that second row. On click of that up arrow just inverse the value of isvisible property.
That is if the property of isvisible is true then it will show the 2 row and if isvisible property is false then will just show you that header.
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="40"/>
</Grid.ColumnDefinitions>
<Label Text="{Binding HeaderText}" Grid.Row="0"
Grid.Column="0" />
<Button Text="Show" Grid.Row="0" Grid.Column="1"
Clicked="LableVisibleButton"/>
<Label
Grid.Row="1" Grid.Grid.ColumnSpan="2"
FormattedText="{Binding FormattedText}" IsVisible="
{Binding LabelVisible}"/>
</ViewCell>
I've done it in notepad so haven't tested it. Moreover I generally don't do xaml. However, this should work. I just added 2 buttons on top of your grid, bound to the same command that toggles a boolean to say wether your grid should be visible or not.
Your ViewModel:
namespace XamlSamples.Models
{
public class Phone
{
public string mobile { get; set; }
public string home { get; set; }
public string office { get; set; }
}
public class Contact
{
public string id { get; set; }
public string name { get; set; }
public string email { get; set; }
public string address { get; set; }
public string gender { get; set; }
public Phone phone { get; set; }
public bool IsCollapsed { get; private set; }
public ICommand ToggleCollapseCommand { get; }
public Contact() => ToggleCollapseCommand = new Command(_ => IsCollapsed = !IsCollapsed);
}
public class ContactList
{
public List<Contact> contacts { get; set; }
}
public class InvertBoolConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) => !(bool)value;
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => !(bool)value;
}
}
Your view:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:models="clr-namespace:XamlSamples.Models;assembly=XamlSamples"
x:Class="XamlSamples.CollapsableListView">
<ContentPage.Resources>
<ResourceDictionary>
<models:InvertBoolConverter x:Key="invertBoolConverter" />
</ResourceDictionary>
</ContentPage.Resources>
<Grid>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Label Grid.Row="0" Margin="10" Text="Display Json Data" FontSize="25" />
<ListView x:Name="listviewConacts" Grid.Row="1" HorizontalOptions="FillAndExpand" HasUnevenRows="True" ItemSelected="listviewContacts_ItemSelected">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Vertical">
<Button Text="Tap to Uncollapse" Command="{Binding ToggleCollapseCommand}" IsVisible="{Binding IsCollapsed}"/>
<Button Text="Tap to Collapse" Command="{Binding ToggleCollapseCommand}" IsVisible="{Binding IsCollapsed, Converter={StaticResource invertBoolConverter}}"/>
<Grid HorizontalOptions="FillAndExpand" Padding="10" IsVisible="{Binding IsCollapsed, Converter={StaticResource invertBoolConverter}}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label Text="{Binding name}" HorizontalOptions="StartAndExpand" Grid.Row="0" TextColor="Blue" FontAttributes="Bold"/>
<Label Text="{Binding email}" HorizontalOptions="StartAndExpand" Grid.Row="1" TextColor="Orange" FontAttributes="Bold"/>
<Label Text="{Binding phone.mobile}" HorizontalOptions="StartAndExpand" Grid.Row="2" TextColor="Gray" FontAttributes="Bold"/>
<BoxView HeightRequest="2" Margin="0,10,10,0" BackgroundColor="Gray" Grid.Row="3" HorizontalOptions="FillAndExpand" />
</Grid>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
<ActivityIndicator x:Name="ProgressLoader" IsRunning="True"/>
</Grid>

XAML template binding in Windows 8

I am walking through Windows Store Sample Application XAML Twitter Client 1, to get the same features in my own application. But i can't get binding work in 1 to 1 sample page.
This is my grid for displaying friends:
<GridView x:Name="FriendsGrid"
Grid.Row="2"
Grid.Column="1"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Margin="10,10,0,0"
ItemsSource="{Binding Friends}"
ItemTemplate="{StaticResource FriendItemTemplate}"
Grid.ColumnSpan="2">
<GridView.DataContext>
<Model:FriendsViewModel/>
</GridView.DataContext>
Template for binding:
<Page.Resources>
<DataTemplate x:Key="FriendItemTemplate">
<Grid Height="200" Width="300">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Image HorizontalAlignment="Left"
Height="80" Width="130"
Margin="10,10,0,0"
VerticalAlignment="Top"
Source="{Binding RealPhoto}"
Stretch="UniformToFill"/>
<TextBlock Grid.Column="1"
HorizontalAlignment="Left"
Margin="10,10,0,0"
TextWrapping="Wrap"
VerticalAlignment="Top"
Height="80"
Width="130"
Text="{Binding FirstName}"/>
<TextBlock HorizontalAlignment="Left"
Margin="10,10,0,0"
Grid.Row="1"
TextWrapping="Wrap"
Text="{Binding LastName}"
VerticalAlignment="Top"
Width="280" Height="80"
Grid.ColumnSpan="2" />
</Grid>
</DataTemplate>
</Page.Resources>
In my code-behind file:
private FriendsViewModel _model;
public MyPage()
{
this.InitializeComponent();
_model = new FriendsViewModel();
FriendsGrid.DataContext = _model;
}
Than i populate model, in application i see exactly the same items count that i`ve added, but items are empty. Using debug i see, that model is not empty.
Also when i am hard-coding values in templates, they are visible.
Test project on GitHub
I forgot to add getters and setters to properties of my model. Initially i had this:
public double Uid;
Than i added {get; set;}
[DataContract]
public class Friend
{
[DataMember(Name = "uid")]
public double Uid { get; set; }
[DataMember(Name="first_name")]
public string FirstName { get; set; }
[DataMember(Name="last_name")]
public string LastName { get; set; }
[DataMember(Name="online")]
public bool Online { get; set; }
[DataMember(Name = "photo")]
public string Photo { get; set; }
public ImageSource RealPhoto { get; set; }
}
To see changes in your view model you need to implement INotifyPropertyChanged. Part of doing this is calling NotifyPropertyChanged on a property if its value changes.

Resources