Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

Data Binding with SQLite

$
0
0

Hello,

I've been for a couple of days trying to solve this issue, I am still new to Xamarin, and would greatly appreciate your help to know what I am doing wrong.

I am trying to display all my data using databinding. But I can't seem to get this straight!

HomePage.xaml

...
<ContentPage Title="Review">
      <ScrollView>
          <ListView x:Name="lvTest" ItemsSource="{Binding WalletManagerItem}">
            <ListView.ItemTemplate>
              <DataTemplate>
                <Grid VerticalOptions="Start" HorizontalOptions="StartAndExpand">
                  <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"></RowDefinition>
                    <RowDefinition Height="*"></RowDefinition>
                  </Grid.RowDefinitions>
                  <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"></ColumnDefinition>
                    <ColumnDefinition Width="*"></ColumnDefinition>
                  </Grid.ColumnDefinitions>
                <StackLayout Padding="5,5,0,0" VerticalOptions="StartAndExpand" Orientation="Vertical">
                  <Label x:Name="lblTeste" BindingContext="{Binding Description}" Grid.Row="0" Grid.Column="0"></Label>
                  <Label BindingContext="{Binding Price}" Grid.Row="0" Grid.Column="1"></Label>
                  <Label BindingContext="{Binding Date}" Grid.Row="1" Grid.Column="0"></Label>
                  <Label BindingContext="{Binding Time}" Grid.Row="1" Grid.Column="1"></Label>
                  </StackLayout>
                </Grid>
              </DataTemplate>
            </ListView.ItemTemplate>
          </ListView>
      </ScrollView>
    </ContentPage>
...

HomePage.cs

public partial class HomePage
{
        public HomePage()
        {
            InitializeComponent();
            BindingContext = new WalletManagerItem();

            // Set timepicker time
            Time.Time = TimeSpan.FromHours(DateTime.Now.Hour) + TimeSpan.FromMinutes(DateTime.Now.Minute);
        }

        private void Button_OnClicked(object sender, EventArgs e)
        {
            var walletManagerItem = (WalletManagerItem) BindingContext;
            App.Database.SaveItem(walletManagerItem);

            lblSuccess.IsVisible = true;
        }

}

WalletManagerItem.cs

namespace WalletManager.Models
{
    public class WalletManagerItem
    {


        public WalletManagerItem()
        {

        }

        [PrimaryKey, AutoIncrement]
        public int ID { get; set; }
        public double Price { get; set; }
        public string Description { get; set; }
        public DateTime Date { get; set; }
        public DateTime Time { get; set; }
    }
}

App.cs

public class App
{
        static WalletManagerDatabase database;

        public static Page GetMainPage()
        {
            return new HomePage();
        }

        public static WalletManagerDatabase Database
        {
            get { return database ?? (database = new WalletManagerDatabase()); }
        }
}

I also have a database manager, to insert and view specific data.


Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>