mercredi 14 juillet 2021

CheckBox in Collection view trying to get values

I have a collection View with an image, name and a checkbox. When I click submit I am trying to collect all the names in a list. I used breakpoints and notice the all my profiles are returned as Selected == false. I think there is a problem with my binding however I am a bit lost as to why.

My ProfileModel:

 public class Profile : INotifyPropertyChanged
{        
    public string UserName { get; set; }       
    public string imageUrl { get; set; }
    private bool _Selected;
    public bool Selected
    {
        get { return _Selected; }
        set
        {
            _Selected = value;
            this.OnPropertyChanged("Selected");
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

My ViewModel:

 public class ProfileViewModel : INotifyPropertyChanged
{
        
    public ObservableCollection<Profile> source;
    public ObservableCollection<Profile> ProfileInfo { get; private set; }
    public ObservableCollection<Profile> EmptyProfileInfo
    {
        get => source;
        set
        {               
            {
                source = value;
                OnPropertyChanged(nameof(EmptyProfileInfo));
            }
        }
    }
    
    public ProfileViewModel ()
    {
        EmptyProfileInfo = new ObservableCollection<Profile>();                   
    }
           
    #region INotifyPropertyChanged
    public event PropertyChangedEventHandler PropertyChanged;
    void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    #endregion
}

My Xaml:

 <CollectionView  ItemsSource="{Binding EmptyProfileInfo}" HeightRequest="200">
                <CollectionView.ItemTemplate>
                    <DataTemplate>
                        <Grid Padding="10" RowDefinitions="80" ColumnDefinitions="120,60,60">
                            <Image Grid.Column="0"
                               Grid.Row="0"
                               x:Name="Image"
                               Source="{Binding imageUrl}"/>
                            <Label Grid.Column="1"
                               Grid.Row="0"
                               Text="{Binding UserName}"
                               FontAttributes="Bold"
                               x:Name="labelpetname" VerticalTextAlignment="Center" HorizontalTextAlignment="Center"/>
                            <CheckBox  Grid.Row="0" Grid.Column="2" HorizontalOptions="End" IsChecked="{Binding Selected}"/>
                        </Grid>
                    </DataTemplate>
                </CollectionView.ItemTemplate>
            </CollectionView>

Profile.cs

private string CreateNameList()
    {
        List<string> NameList = new List<string>();
        foreach (var profile in ProfileViewModel.EmptyProfileInfo)
        {               
            if (profile.Selected)
            {
                NameList.Add(profile.UserName);
            }
        }
        
    }



Aucun commentaire:

Enregistrer un commentaire