Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

ListView items do not update

$
0
0

Hi all,
I update my binding object but the ListView doesn't update. I have to leave the page and reopen it.

My object:

`public class Variant
{
public Command TrashCommand { set; get; }
public Command AddCommand { set; get; }
public Variant()
{
TrashCommand = new Command(OnTrashTapped);
AddCommand = new Command(OnVariantTapped);
}

    private void OnVariantTapped(object obj)
    {
        var item = obj as Variant;
        item.Qta = item.Qta + 1;
    }

    private void OnTrashTapped(object obj)
    {
        var variant = obj as Variant;
        variant.Qta = variant.Qta - 1;
        if (variant.Qta < 0)
            variant.Qta = 0;
    }

    public string Id { get; set; }
    public string ItemId { set; get; }
    public string Text { get; set; }
    public int Qta { set; get; }
    public decimal Price { set; get; }

    public bool BadgeIsVisible
    {
        get
        {
            if (Qta > 0)
                return true;
            return false;
        }
    }
}`

My ViewModel:

`public class ItemDetailViewModel : BaseViewModel
{
public Item Item { get; set; }
public ObservableCollection Variants { set; get; }

    public ItemDetailViewModel(Item item = null)
    {
        Title = item?.Text;
        Item = item;
        Variants = new ObservableCollection<Variant>();
        foreach (var obj in item.Variants)
        {
            Variants.Add(obj as Variant);
        }
    }
}`

BaseViewModel.cs

`public class BaseViewModel : INotifyPropertyChanged
{
public IDataStore DataStore => DependencyService.Get<IDataStore>();

    bool isBusy = false;
    public bool IsBusy
    {
        get { return isBusy; }
        set { SetProperty(ref isBusy, value); }
    }

    string title = string.Empty;
    public string Title
    {
        get { return title; }
        set { SetProperty(ref title, value); }
    }

    protected bool SetProperty<T>(ref T backingStore, T value,
        [CallerMemberName]string propertyName = "",
        Action onChanged = null)
    {
        if (EqualityComparer<T>.Default.Equals(backingStore, value))
            return false;

        backingStore = value;
        onChanged?.Invoke();
        OnPropertyChanged(propertyName);
        return true;
    }

    #region INotifyPropertyChanged
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
    {
        var changed = PropertyChanged;
        if (changed == null)
            return;

        changed.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    #endregion
}`

My Page.cs

`[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ItemDetailPopup : PopupPage
{
ItemDetailViewModel viewModel;

    public ItemDetailPopup()
    {
        InitializeComponent();
    }
    public ItemDetailPopup(Item item)
    {
        InitializeComponent();
        BindingContext = viewModel = new ItemDetailViewModel(item);
    }
}`

My Page.xaml

<pages:PopupPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:d="http://xamarin.com/schemas/2014/forms/design" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:pages="clr-namespace:Rg.Plugins.Popup.Pages;assembly=Rg.Plugins.Popup" xmlns:animations="clr-namespace:Rg.Plugins.Popup.Animations;assembly=Rg.Plugins.Popup" mc:Ignorable="d" x:Class="XamarinAPP.Views.Popup.ItemDetailPopup"> <pages:PopupPage.Animation> <animations:ScaleAnimation PositionIn="Bottom" PositionOut="Center" ScaleIn="1.2" ScaleOut="0.8" DurationIn="400" DurationOut="300" EasingIn="SinOut" EasingOut="SinIn" HasBackgroundAnimation="True"/> </pages:PopupPage.Animation> <StackLayout Orientation="Vertical" HorizontalOptions="FillAndExpand" BackgroundColor="White" Margin="0, 0, 0, 0"> <Image Source="pizza.png" HorizontalOptions="FillAndExpand" HeightRequest="50" /> <Label Text="{Binding Item.Text}" HorizontalTextAlignment="Center" d:Text="{Binding .}" LineBreakMode="NoWrap" Style="{DynamicResource ListItemTextStyle}" FontSize="20" /> <Label Text="{Binding Item.Price, StringFormat='{0:C}'}" HorizontalTextAlignment="Center" LineBreakMode="NoWrap" Style="{DynamicResource ListItemTextStyle}" FontSize="18" /> <Label Text="{Binding Item.Description}" HorizontalTextAlignment="Center" LineBreakMode="NoWrap" Style="{DynamicResource ListItemTextStyle}" FontSize="16" /> <ListView HasUnevenRows="True" x:Name="listViewVariants" IsPullToRefreshEnabled="False" CachingStrategy="RecycleElement" ItemsSource="{Binding Variants}" > <ListView.Header> <Label Text="Varianti" BackgroundColor="LightGray" HorizontalOptions="FillAndExpand" LineBreakMode="NoWrap" VerticalTextAlignment="End" FontSize="18" HeightRequest="45" Padding="10, 0, 0, 5" /> </ListView.Header> <ListView.ItemTemplate> <DataTemplate> <ViewCell> <StackLayout Padding="10"> <StackLayout> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="60*" /> <ColumnDefinition Width="20*" /> <ColumnDefinition Width="20*" /> </Grid.ColumnDefinitions> <BoxView x:Name="itemCount" IsVisible="{Binding BadgeIsVisible}" Grid.Column="0" BackgroundColor="Green" VerticalOptions="Center" HorizontalOptions="Start" WidthRequest="16" HeightRequest="16" CornerRadius="50"> </BoxView> <Label Text="{Binding Qta}" IsVisible="{Binding BadgeIsVisible}" Grid.Column="0" VerticalOptions="Center" HorizontalOptions="Start" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" FontAttributes="Bold" Padding="0,0,0,4" WidthRequest="16" HeightRequest="16" TextColor="White" FontSize="11"/> <Label Grid.Column="0" Text="{Binding Text}" d:Text="{Binding .}" LineBreakMode="NoWrap" Style="{DynamicResource ListItemTextStyle}" FontSize="16" VerticalTextAlignment="Center" Padding="20,0,0,0"/> <Label Grid.Column="1" Text="{Binding Price, StringFormat='{0:C}'}" d:Text="{Binding .}" LineBreakMode="NoWrap" Style="{DynamicResource ListItemTextStyle}" HorizontalTextAlignment="End" VerticalTextAlignment="Center" FontSize="16" TextColor="Red" /> <Image Source="trash.png" Grid.Column="2" WidthRequest="32" IsVisible="{Binding BadgeIsVisible}"> <Image.GestureRecognizers> <TapGestureRecognizer Command="{Binding TrashCommand}" CommandParameter="{Binding .}"/> </Image.GestureRecognizers> </Image> </Grid> </StackLayout> <StackLayout.GestureRecognizers> <TapGestureRecognizer Command="{Binding AddCommand}" CommandParameter="{Binding .}"/> </StackLayout.GestureRecognizers> </StackLayout> </ViewCell> </DataTemplate> </ListView.ItemTemplate> </ListView> </StackLayout> </pages:PopupPage>


Viewing all articles
Browse latest Browse all 204402

Trending Articles



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