I Need to bind data from a inner list to view in listview grouping?

RichyRichy Member ✭✭
edited January 15 in Xamarin.Forms

// Model

public class StudyParameterGroupModel :  List<StudyParameterGroupOptionsModel> 
{
    public string GroupDisplayText { get; set; }
    public List<StudyParameterGroupOptionsModel> DiseaseOptions { get; set; } 
}

public class StudyParameterGroupOptionsModel
{
    public string ParameterDisplayText { get; set; }
    public int SubDiseaseId { get; set; }
    public bool IsSelected { get; set; }
}

// Mapping data from rest response

public List<StudyParameterGroupModel> _studyParameterGroupList;
    public List<StudyParameterGroupModel> StudyParameterGroupList
    {
        get
        {
            return _studyParameterGroupList;
        }
        set
        {
            _studyParameterGroupList = value;
            OnPropertyChanged("StudyParameterGroupList");
        }
    }

IRestResponse<List<StudyParameterGroupDTO>> responseDto = client.Execute<List<StudyParameterGroupDTO>>(request);

if (response.StatusCode == HttpStatusCode.OK)
{
       StudyParameterGroupList  = responseDto.Data.Select(resp => new StudyParameterGroupModel()

            GroupDisplayText = resp.DisplayText,
            DiseaseOptions = resp.Options
                                 .Select(opt => new StudyParameterGroupOptionsModel()
                                 {
                                     ParameterDisplayText = opt.DisplayText,
                                    SubDiseaseId = opt.SubDiseaseId,
                                 }).ToList(),
        }).ToList();
}

// XAML

 ~<ListView x:Name="list" 
                        ItemsSource="{Binding StudyParameterGroupList, Mode=TwoWay}" 
                        IsGroupingEnabled="True"
                        GroupDisplayBinding="{Binding GroupName}"
                        HasUnevenRows="True">

                        <ListView.GroupHeaderTemplate>
                            <DataTemplate>
                                <ViewCell Height="25">
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="Auto"/>
                                            <ColumnDefinition Width="Auto"/>
                                        </Grid.ColumnDefinitions>
                                        <Label Grid.Row="0" Text="{Binding GroupDisplayText }" 
                                             FontSize="Medium"
                                             FontFamily="{StaticResource RegularFont}"
                                             TextColor="White" VerticalOptions="Center"/>
                                        <Label VerticalTextAlignment="Start" VerticalOptions="Start" 
                                               HorizontalOptions="Start" Grid.Column="1" Text="*"
                                               TextColor="Red"
                                               FontSize="Medium"
                                               FontFamily="{StaticResource RegularFont}" />
                                    </Grid>
                                </ViewCell>
                            </DataTemplate>

                        </ListView.GroupHeaderTemplate>
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="Auto"/>
                                    </Grid.ColumnDefinitions>
                                    <telerikPrimitives:RadCheckBox x:Name="checkbox" Grid.Row="0" IsChecked="{Binding IsSelected, Mode=TwoWay}" />
                                    <Label Text="{Binding Parameter, Mode=TwoWay}"
                                                           VerticalTextAlignment="Center" VerticalOptions="Center" HorizontalOptions="Center" Grid.Column="1"
                                                           FontSize="Medium"
                                                           FontFamily="{StaticResource RegularFont}"/>
                                </Grid>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>~

Data is in the StudyParameterGroupList, but not appearing in view.

Answers

  • XigmondXigmond USMember ✭✭✭

    Each ListView Item has a BindingContext dynamically assigned to.
    and is accessible like this :

    var context = object.BindingContext as ModelClass;

    and you could use the values in it like this:

    var p = (object.BindingContext as ModelClass).Property;

    good luck

    Love Xamarin : <3

  • ColeXColeX Member, Xamarin Team Xamurai

    Try to set ListView.ItemsSource in code in Page.

    list.ItemsSource = StudyParameterGroupList ; 
    
  • RichyRichy Member ✭✭

    @ColeX said:
    Try to set ListView.ItemsSource in code in Page.

    list.ItemsSource = StudyParameterGroupList ; 
    

    Doing it in MVVM

  • ColeXColeX Member, Xamarin Team Xamurai
    edited January 15

    Could you provide the complete code used in the page ?

  • RichyRichy Member ✭✭
    edited January 15

    namespace Cureitt.Mobile.Patient.ViewModels
    {
    public class ProfileViewModel : ValidationBase, INotifyPropertyChanged
    {
    private Page _page;

        public ObservableCollection<Grouping<string, StudyParameterGroupOptionsModel>> StudyParameterGroupOptionsGrouped { get; set; }
    
        public ObservableCollection<StudyParameterGroupOptionsModel> StudyParameterGroupOptions { get; set; }
    
        public List<StudyParameterGroupModel> _studyParameterGroupList;
        public List<StudyParameterGroupModel> StudyParameterGroupList
        {
            get
            {
                return _studyParameterGroupList;
            }
            set
            {
                _studyParameterGroupList = value;
                Height = (_studyParameterGroupList.Count * 50) + (_studyParameterGroupList.Count * 20);
                OnPropertyChanged("StudyParameterGroupList");
            }
        }
    
        int height;
        public int Height
        {
            get
            {
                return height;
            }
            set
            {
                height = value;
                OnPropertyChanged("Height");
            }
        }
    
        public ProfileViewModel(Page page)
        {
        GetParameterGroupList(1);
            _page = page;
        }
    
        public List<StudyParameterGroupOptionsModel> GetSelectedParameters()
        {
            //DiseaseParameterLists = StudyParameterGroupList.SelectMany(res => res.DiseaseOptions).Where(model => model.IsSelected).ToList();
            //return temp.Where(model => model.IsSelected).ToList();
    
    
            //DiseaseParameterLists = StudyParameterGroupList.SelectMany(res => res.DiseaseOptions).ToList().Where(model => model.IsSelected).ToList();
    
            if (StudyParameterGroupList != null)
                return StudyParameterGroupList.SelectMany(res => res.DiseaseOptions).ToList().Where(model => model.IsSelected).ToList();
            else
                return null;
        }
    
        private async Task GetParameterGroupList(int subid)
        {
        var studyResult = await scope.Resolve<IStudyParameterGroupsController>().GetAllStudyParameterGroups("Mobile");
        if (!studyResult.isError)
                {
                    var allParameterGroups = (studyResult.responseDto.Data.Select(resp => new StudyParameterGroupModel()
                    {
                        GroupName = resp.Name,
                        GroupDisplayText = resp.DisplayText,
                        GroupType = resp.GroupType,
                        DiseaseOptions = resp.Options
                                             .Select(opt => new StudyParameterGroupOptionsModel()
                                             {
                                                 ParameterId = opt.ParameterId,
                                                 ParameterDisplayText = opt.DisplayText,
                                                 ParameterName = opt.Name,
                                                 SubDiseaseId = opt.SubDiseaseId,
                                             }).ToList(),
                    }).ToList());
                    StudyParameterGroupList= allParameterGroups;
                }
    
    
            IRestResponse<List<StudyParameterGroupDTO>> responseDto = client.Execute<List<StudyParameterGroupDTO>>(request);
    
            if (responseDto.StatusCode == HttpStatusCode.OK)
            {
                for (int grp = 0; grp < responseDto.Data.Count; grp++)
                {
                    var asset = new StudyParameterGroupModel();
                    asset.GroupName = responseDto.Data[grp].Name;
                    asset.GroupDisplayText = responseDto.Data[grp].DisplayText;
                    asset.GroupType = responseDto.Data[grp].GroupType;
                    asset.DiseaseOptions = new List<StudyParameterGroupOptionsModel>();
    
                    for (int opt = 0; opt < responseDto.Data[grp].Options.Count; opt++)
                    {
                        asset.DiseaseOptions.Add(new StudyParameterGroupOptionsModel()
                        {
                            ParameterId = responseDto.Data[grp].Options[opt].ParameterId,
                            ParameterDisplayText = responseDto.Data[grp].Options[opt].DisplayText,
                            ParameterName = responseDto.Data[grp].Options[opt].Name,
                            SubDiseaseId = responseDto.Data[grp].Options[opt].SubDiseaseId,
                        });
                    }
    
                    var sorted = new Grouping<string, StudyParameterGroupOptionsModel>(asset.GroupDisplayText, asset.DiseaseOptions);
    
                    StudyParameterGroupOptionsGrouped.Add(sorted);
                }
        }
    
        public async Task loadProfile()
        {
            try
            {
                using (var scope = App.Container.BeginLifetimeScope())
                {
                    int _usrID = Int32.Parse(Preferences.Get("UserID", "default_value"));
                    var profileResult =
                        await scope.Resolve<IPatientProfileController>().GetPatientProfile("Mobile", _usrID);
                    if (!profileResult.isError)
                    {
                        BProfile = profileResult.basicProfileModel; 
    
                        if (BProfile.YearOfBirth != 0)
                        {
                            await GetParameterGroupList(BProfile.SubDisease.Id);
                        }
                    }
                    else
                    {
                        UserDialogs.Instance.Alert("Error fetching profile");
                    }
                }
            }
            catch (Exception ex)
            {
    
            }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    

    }

    @ColeX said:
    Could you provide the complete code used in the page ?

  • ColeXColeX Member, Xamarin Team Xamurai

    It's better to upload a mini, basic sample project on forum , we need to reproduce the issue for further troubleshooting.

Sign In or Register to comment.