question

AnudeepYerramsetty-2011 avatar image
0 Votes"
AnudeepYerramsetty-2011 asked RobCaplan edited

Listview is not updating when viewmodel object changes

<?xml version="1.0" encoding="UTF-8" ?>
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="AudioDigestMembership.Views.MainContent.Filter.FilterPageDetail"
Shell.NavBarHasShadow="False"
Shell.NavBarIsVisible="True"
xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core" xmlns:autocomplete="clr-namespace:Syncfusion.SfAutoComplete.XForms;assembly=Syncfusion.SfAutoComplete.XForms"
ios:NavigationPage.IsNavigationBarTranslucent="true"
ios:Page.UseSafeArea="true"
BackgroundColor="{AppThemeBinding Light=#FFFFFF, Dark=#121212}"
xmlns:ListCollection="clr-namespace:System.Collections.Generic;assembly=netstandard" xmlns:controls="clr-namespace:AudioDigestMembership.Controls" xmlns:xforms="clr-namespace:Syncfusion.ListView.XForms;assembly=Syncfusion.SfListView.XForms" xmlns:converter="clr-namespace:AudioDigestMembership.Converters"
>
<ContentPage.Resources>
<ResourceDictionary>
<converter:SelectionBoolToImageConverter x:Key="BoolToImageConverter" />
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
<Grid RowDefinitions="44, 1*">
<autocomplete:SfAutoComplete x:Name="autoComplete"
Grid.Row="0"
BorderColor="{AppThemeBinding Light=#000, Dark=#fff}"
ShowBorder="True"
Watermark="Search by name, specialty, topic"
WatermarkColor="{AppThemeBinding Light=#386384, Dark=#f6f6f6}"
TextColor="{AppThemeBinding Light=#386384, Dark=#f6f6f6}"
TextSize="15" >
<autocomplete:SfAutoComplete.AutoCompleteSource>
<ListCollection:List x:TypeArguments="x:String">
<x:String>India</x:String>
<x:String>Uganda</x:String>
<x:String>Ukraine</x:String>
<x:String>Canada</x:String>
<x:String>United Arab Emirates</x:String>
<x:String>France</x:String>
<x:String>United Kingdom</x:String>
<x:String>China</x:String>
<x:String>United States</x:String>
<x:String>Japan</x:String>
<x:String>Angola</x:String>
</ListCollection:List>
</autocomplete:SfAutoComplete.AutoCompleteSource>
</autocomplete:SfAutoComplete>

         <controls:MyListView
                 Grid.Row="1"
                 x:Name="listView"
                 VerticalOptions="FillAndExpand"
                 HorizontalOptions="FillAndExpand"
                 ItemsSource="{Binding DetailModel, Mode=TwoWay}"
                 BackgroundColor="{AppThemeBinding Light=#FFFFFF, Dark=#121212}"
                 SelectionMode="Multiple"
                 SelectionBackgroundColor="{AppThemeBinding Light=#FFFFFF, Dark=#121212}"
                 ItemTapped="listView_ItemTapped"
                 AutoFitMode="Height"
                 ItemSize="50"
             >
             <controls:MyListView.ItemTemplate>
                 <DataTemplate>
                     <Grid RowDefinitions="*, 1" VerticalOptions="FillAndExpand"
                           HorizontalOptions="FillAndExpand" x:Name="MultiSelectCellGrid">
                         <Grid Padding="20, 20" Grid.Row="0" ColumnSpacing="20" ColumnDefinitions="30, *"
                               VerticalOptions="FillAndExpand"
                               HorizontalOptions="FillAndExpand">
                             <Image Grid.Column="0"
                                     Source="{Binding IsSelected, Converter={StaticResource BoolToImageConverter}}"/>
                             <Label  Grid.Column="1"
                                     Text="{Binding SpecialtyName}"
                                    TextColor="{AppThemeBinding Light=#121212, Dark=#FFFFFF}"
                                    VerticalOptions="FillAndExpand"
                                    HorizontalOptions="FillAndExpand"
                                    VerticalTextAlignment="Center"
                                    HorizontalTextAlignment="Start"
                                />
                         </Grid>
                         <BoxView Grid.Row="1" Margin="0" BackgroundColor="{AppThemeBinding Light=#D4DDE5, Dark=#585858}" />
                     </Grid>
                 </DataTemplate>
             </controls:MyListView.ItemTemplate>
         </controls:MyListView>
     </Grid>
 </ContentPage.Content>

</ContentPage>







using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using ApiSDK;
using AudioDigestMembership.Utils;
using AudioDigestMembership.ViewModels.Library.FilterandSort;
using Xamarin.Essentials;
using Xamarin.Forms;

namespace AudioDigestMembership.Views.MainContent.Filter
{
public partial class FilterPageDetail : ContentPage
{
FilterPageDetailViewModel ViewModel = new FilterPageDetailViewModel();
public FilterPageDetail()
{
InitializeComponent();
this.BindingContext = this.ViewModel;
}

     protected override void OnAppearing()
     {
         base.OnAppearing();
         ViewModel.OnAppearing();
     }

     protected override void OnDisappearing()
     {
         ViewModel.OnDisappearing();
         base.OnDisappearing();
     }

     private async void listView_ItemTapped(object sender, Syncfusion.ListView.XForms.ItemTappedEventArgs e)
     {
         try
         {
             var viewModel = (SpecialtyDetail)e.ItemData;
             viewModel.IsSelected = !viewModel.IsSelected;
             viewModel.SpecialtyName = "test";
             int index = this.ViewModel.DetailModel.IndexOf(viewModel);
             var updateList = this.ViewModel.DetailModel;
             updateList[index] = viewModel;
             listView.ItemsSource = new ObservableCollection<SpecialtyDetail>() { viewModel };
             //updateList.Clear();
             //this.ViewModel.DetailModel = new ObservableCollection<SpecialtyDetail>() { viewModel };
         }
         catch (Exception ex)
         {
             await App.Current.MainPage.DisplayAlert("Alert", ex.Message, "OK");
         }
     }
 }

}






using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Web;
using System.Windows.Input;
using ApiSDK;
using AudioDigestMembership.Interfaces;
using AudioDigestMembership.Models;
using Newtonsoft.Json;
using Xamarin.Forms;

namespace AudioDigestMembership.ViewModels.Library.FilterandSort
{
public class FilterPageDetailViewModel: BaseViewModel
{
public ICommand ListItemTappedCommand { protected set; get; }
private ObservableCollection<SpecialtyDetail> detailModel;
public ObservableCollection<SpecialtyDetail> DetailModel
{
set
{
detailModel = value;
OnPropertyChanged();
}
get
{
return detailModel;
}
}

     public FilterPageDetailViewModel()
     {
         ListItemTappedCommand = new Command(async (parameter) =>
         {
             Console.WriteLine(parameter);
         });
     }

     public void OnAppearing()
     {
         this.FetchSpecialities();
     }

     public void OnDisappearing()
     {

     }


     public async void FetchSpecialities()
     {
         DependencyService.Get<ILodingPageService>().ShowLoadingPage();
         try
         {
             this.DetailModel = await _mobileApi.GetAllSpecialties();
         }
         catch (Exception ex)
         {
             await App.Current.MainPage.DisplayAlert("Alert", ex.Message, "OK");
         }
         DependencyService.Get<ILodingPageService>().HideLoadingPage();
     }
     //public void ApplyQueryAttributes(IDictionary<string, string> query)
     //{
     //    // The query parameter requires URL decoding.
     //    this.DetailModel = JsonConvert.DeserializeObject<FIlterModel>(HttpUtility.UrlDecode(query["DetailItemModel"]));
     //}

 }

}



I tried changing viewmodel object - DetailModel in listview tap event. But it is not updating in listview

i even changed DetailModel parameters but it is not updating to new parameters

dotnet-xamarin
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi, AnudeepYerramsetty-2011. I created a basic demo to test the function with Xamarin.Forms.ListView, it works as expected. It seems there is nothing wrong with the viewModel and item tapped event. You could try to test the code with Xamarin.Forms to vertify that. Try to refer to the doc about the 'Syncfusion.ListView' to check if there is something missed. If it is a potential issue with the nuget, please report the issue on its portal.

0 Votes 0 ·

0 Answers