Hello,
I have a simple listview, one label and one entry cell. Whenever i type something in entry cell value, underlying ObservableCollection is not updating.
Here is my code.
----------------- Model
public class TicketItem : INotifyPropertyChanged
{
[PrimaryKey, AutoIncrement]
public String Description { get; set; }
private Double? _quantityNeeded;
public Double? QuantityNeeded
{
get { return _quantityNeeded; }
set { _quantityNeeded = value; OnPropertyChanged("QuantityNeeded"); }
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
------------------ ViewModel
public class ItemsViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private ObservableCollection<TicketItem> _itemSignsList;
public ObservableCollection<TicketItem> ItemSignsList
{
get { return _itemSignsList; }
set { _itemSignsList = value; OnPropertyChanged("ItemSignsList"); }
}
public ItemsViewModel()
{
ItemSignsList = new ObservableCollection<TicketItem>(DataSource.GetData());
ItemSignsList.CollectionChanged += ItemSignsList_CollectionChanged;
}
private void ItemSignsList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null)
foreach (TicketItem item in e.NewItems)
item.PropertyChanged += TicketItem_PropertyChanged;
if (e.OldItems != null)
foreach (TicketItem item in e.OldItems)
item.PropertyChanged -= TicketItem_PropertyChanged;
}
void TicketItem_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
// Never comes here
if (e.PropertyName == "QuantityNeeded")
{
}
}
}
--------------------- Xaml.cs
public partial class ItemSignsPage : ContentPage
{
ItemsViewModel istvm;
bool IsTabLoaded { get; set; }
public ItemSignsPage()
{
InitializeComponent();
}
protected override void OnAppearing()
{
base.OnAppearing();
lvItemSigns.ItemTemplate = new DataTemplate(typeof(DataCell));
istvm = new ItemsViewModel();
this.BindingContext = istvm;
lvItemSigns.ItemsSource = istvm.ItemSignsList;
}
class DataCell : ViewCell
{
public DataCell()
{
var grid = new Grid();
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(10, GridUnitType.Star) });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(2, GridUnitType.Star) });
var lblItemDesc = new Label();
lblItemDesc.SetBinding(Label.TextProperty, "Description");
grid.Children.Add(lblItemDesc, 0, 0);
var entryQtyDelRet = new Entry();
entryQtyDelRet.SetBinding(Entry.TextProperty, new Binding("QuantityNeeded", BindingMode.TwoWay));
grid.Children.Add(entryQtyDelRet, 2, 0);
View = grid;
}
}
}
Please help. If there is no way of ObservableCollection automatically updating if listview entrycell is changed then is there a way to get the value of entry which is changed preferably the object itself for that row.
Thanks,
Sai