Giter Site home page Giter Site logo

syncfusionexamples / add-or-remove-listview-xamarin Goto Github PK

View Code? Open in Web Editor NEW
0.0 5.0 0.0 1.8 MB

This repository contains sample about how to add or remove an item from Xamarin.Forms ListView (SfListView)

C# 100.00%
listview sflistview xamarin xamarin-forms mvvm binding viewmodel add-item remove-item-from-listview

add-or-remove-listview-xamarin's Introduction

How to add or remove an item from Xamarin.Forms ListView (SfListView)

You can add or remove items from ItemsSource with a bound collection from ViewModel in Xamarin.Forms SfListView.

XAML

ViewModel commands bound to view to add or remove item.

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:ListViewXamarin"
             xmlns:syncfusion="clr-namespace:Syncfusion.ListView.XForms;assembly=Syncfusion.SfListView.XForms"
             x:Class="ListViewXamarin.MainPage">
    <ContentPage.BindingContext>
        <local:ContactsViewModel/>
    </ContentPage.BindingContext>
 
    <ContentPage.Content>
        <StackLayout>
            <Grid HeightRequest="50" >
                <Button Text="Add item" Grid.Column="0" Command="{Binding AddCommand}"/>
                <Button Text="Delete last item" Grid.Column="1" Command="{Binding DeleteCommand}}"/>
            </Grid>
            <syncfusion:SfListView x:Name="listView" 
                                   ItemSpacing="1" 
                                   ItemSize="50"
                                   ItemsSource="{Binding contactsinfo}">
                <syncfusion:SfListView.ItemTemplate >
                    <DataTemplate>
                        <Grid x:Name="grid" RowSpacing="0">
                            <Grid RowSpacing="0">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="70" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <Image Source="{Binding ContactImage}" VerticalOptions="Center" HorizontalOptions="Center" HeightRequest="50" WidthRequest="50"/>
                                <Grid Grid.Column="1" RowSpacing="1" Padding="10,0,0,0" VerticalOptions="Center">
                                    <Label LineBreakMode="NoWrap" TextColor="#474747" Text="{Binding ContactName}"/>
                                    <Label Grid.Row="1" Grid.Column="0" TextColor="#474747" LineBreakMode="NoWrap" Text="{Binding ContactNumber}"/>
                                </Grid>
                            </Grid>
                        </Grid>
                    </DataTemplate>
                </syncfusion:SfListView.ItemTemplate>
            </syncfusion:SfListView>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

C#

Object added to the collection of ViewModel.

public class ContactsViewModel
{
  public Command AddCommand { get; set; }
 
    public ContactsViewModel ()
    {
        AddCommand = new Command(OnAddTapped);
    }
 
    private void OnAddTapped(object obj)
  {
      var details = new Contacts()
      {
          ContactNumber = random.Next(100, 400).ToString() + "-" + random.Next(500, 800).ToString() + "-" + random.Next(1000, 2000).ToString(),
          ContactName = CustomerNames[random.Next(1, 50)],
          ContactImage = ImageSource.FromResource("ListViewXamarin.Images.Image" + random.Next(0, 28) + ".png", typeof(MainPage)),
      };
      contactsinfo.Add(details);
  }
}

C#

Object removed from the collection of ViewModel.

public class ContactsViewModel
{
  public Command DeleteCommand { get; set; }
 
    public ContactsViewModel ()
    {
        DeleteCommand = new Command(OnDeleteTapped);
    }
    private void OnDeleteTapped(object obj)
  {
      if (contactsinfo.Count > 0)
          contactsinfo.Remove(contactsinfo[contactsinfo.Count - 1]);
      else
          App.Current.MainPage.DisplayAlert("Alert", "There is no item in the list", "OK");
  }
}

add-or-remove-listview-xamarin's People

Contributors

jayaleshwari avatar sarubala20 avatar syncfusionbuild avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.