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

ListView not update

$
0
0

Hello Developers,

I want to update my ListView at TextChanged event of Entry.
I tried some things bu not worked.

This is my model

 public class Model
    {
        public string Number { get; set; }
        public string Price { get; set; }
        public string Name { get; set; }
    }

This is my view model

    public class ViewModel : INotifyPropertyChanged
    {
        public ViewModel()
        {
            GetItemList();
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        private ObservableCollection<Model> _itemList;
        public ObservableCollection<Model> ItemList
        {
            get { return _itemList; }
            set
            {
                _itemList = value;
                NotifyPropertyChanged("ItemList");
            }
        }

        public void GetItemList()
        {
            ItemList = new ObservableCollection<Model>
            {
                new Model
                {
                    Name = "Tenderloin Pepper",
                    Number = "65",
                    Price="50.00"
                },
                new Model
                {
                    Name = "Tenderloin Bea",
                    Number = "67",
                    Price="55.00"
                },
                new Model
                {
                    Name = "Pepsi Coke",
                    Number = "102",
                    Price="5.00"
                },};
        }
    }

This is my xaml Code

 <Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="70"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid Grid.Row="0" ColumnSpacing="20" Margin="5">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="18*"/>
            <ColumnDefinition Width="58*"/>
            <ColumnDefinition Width="24*"/>
        </Grid.ColumnDefinitions>
        <Entry Grid.Column="0" x:Name="txtNumber" Placeholder="Enter Number" TextChanged="TxtNumber_TextChanged" />
        <Entry Grid.Column="1" x:Name="txtName" Placeholder="Enter name" TextChanged="TxtName_TextChanged" />
        <Entry Grid.Column="2" x:Name="txtPrice" Placeholder="Enter price" TextChanged="TxtPrice_TextChanged" />
     </Grid>               
    <ListView Grid.Row="1" x:Name="ItemList" ItemsSource="{Binding ItemList}" HasUnevenRows="true"   
                           ItemTapped="ItemList_ItemTapped" CachingStrategy="RecycleElement">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell >
                    <Grid ColumnSpacing="20" >
                       <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="18*"/>
                            <ColumnDefinition Width="58*"/>
                            <ColumnDefinition Width="24*"/>
                        </Grid.ColumnDefinitions>
                        <Label Grid.Column="0" Text="{Binding Number}" VerticalTextAlignment="Center" HorizontalTextAlignment="Center" />
                        <Label Grid.Column="0" Text="{Binding Name}" VerticalTextAlignment="Center" HorizontalTextAlignment="Center" />
                        <Label Grid.Column="0" Text="{Binding Price}" VerticalTextAlignment="Center" HorizontalTextAlignment="Center" />
                    </Grid>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>   
 </Grid>  

This is Xam.cs Code

    public partial class ItemView : ContentView
    {
        ViewModel viewModel;
        public ItemView()
        {
            InitializeComponent();
            BindingContext = viewModel = new ViewModel();
        }

        private void ItemList_ItemTapped(object sender, ItemTappedEventArgs e)
        {
            var item = e.Item as Model;
            txtName.Text = item.Name;
            txtNumber.Text = item.Number;
            txtPrice.Text = item.Price;

        }

        private void TxtName_TextChanged(object sender, TextChangedEventArgs e)
        {  
            if (string.IsNullOrEmpty(txtName.Text))
            {
                ItemList.SelectedItem = null;
            }
            else
            { 
                var update_item = viewModel.ItemList.FirstOrDefault(i => i.Number == txtNumber.Text);
                if (update_item != null)
                {
                    ItemList.ItemsSource = null;
                    update_item.Name = txtName.Text;
                    update_item.Price = string.Format("{0:0.00}", Convert.ToDouble(txtPrice.Text));
                    ItemList.ItemsSource = viewModel.ItemList;
                }
            }
        }  

        private void TxtPrice_TextChanged(object sender, TextChangedEventArgs e)
        {
           if (string.IsNullOrEmpty(txtName.Text))
            {
                ItemList.SelectedItem = null;
            }
            else
            { 
                var update_item = viewModel.ItemList.FirstOrDefault(i => i.Number == txtNumber.Text);
                if (update_item != null)
                {
                    ItemList.ItemsSource = null;
                    update_item.Name = txtName.Text;
                    update_item.Price = string.Format("{0:0.00}", Convert.ToDouble(txtPrice.Text));
                    ItemList.ItemsSource = viewModel.ItemList;
                }
            }
        }
    }

Viewing all articles
Browse latest Browse all 89864

Trending Articles



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