1
votes

iam trying to create xmarin forms application witch use a collection view that contain two item ( label = binding name & switch = binding switched ) but i have a problem with the switch its not update without Scrolling the collection i use Model-View-ViewModel to bind my data and do operation that`s my xaml :

<CollectionView x:Name="GroupsCV"  
                        ItemsSource="{Binding Groups}" 
                        SelectionMode="Multiple"
                        EmptyView="No Data"
                        SelectionChangedCommand="{Binding SelectedCommand}"
                        SelectionChangedCommandParameter="{Binding Source={x:Reference GroupsCV}}">
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <Grid Padding="15">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="30"/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="0.1*"/>
                        </Grid.ColumnDefinitions>
                        <Switch Style="{StaticResource SwitchStyle}" IsEnabled="False" IsToggled="{Binding Switched}" Grid.Column="0"/>
                        <Label Text="{Binding Name}" Grid.Column="1" Style="{StaticResource LabelStyle}"/>
                    </Grid>
                </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>

and this my code from View Model:

public GroupsViewModel()
    {
        datarepo.conn.CreateTable<Group>();
        _groups = datarepo.conn.Table<Group>().ToList();
        //GroupsCount = Groups.Count();
    }
    private IEnumerable<Group> _groups;
    public IEnumerable<Group> Groups
    {
        get => _groups;
        set 
        {
            if (_groups != value)
            {
                _groups = value;
                OnPropertyChanged(nameof(Groups));
            }
        }
    }

public ICommand SelectedCommand
    {
        get
        {
            return new Command<CollectionView>((s) =>
            {
                var x = s.SelectedItems.Cast<Group>();
                foreach (var e in x)
                {
                    foreach (var z in Groups)
                    {
                        if (z.Link == x..Link)
                        {
                            if (z.Switched == false)
                            {
                                z.Switched = true;
                            }
                            else
                            {
                                z.Switched = false;
                            }
                            break;
                        }
                    }
                }
                OnPropertyChanged(nameof(Groups));
            });
        }
    }

All Thing Work fine only the ui doesn`t update !

Update With Group Code

[Table("Group")]
public class Group
{
    [PrimaryKey, AutoIncrement()]
    public int? Id { get; set; }
    [Unique]
    public string Link { get; set; }
    public string Name { get; set; }
    public string MemberCount { get; set; } = null;
    public bool Switched { get; set; }

}
1
does Group implement INotifyPropertyChanged?Jason
yes its implementEzz
Please post the code for GroupJason
Did you solved it?Anand
@Jason updated with the codeEzz

1 Answers

1
votes

Can you change your Group class like this?

[Table("Group")]
public class Group: INotifyPropertyChanged
{
 public event PropertyChangedEventHandler PropertyChanged;
            private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
    [PrimaryKey, AutoIncrement()]
    public int? Id { get; set; }
    [Unique]
    public string Link { get; set; }
    public string Name { get; set; }
    public string MemberCount { get; set; } = null;
    public bool Switched { get; set; }

}