0

I have a checkbox in my ListView. and the property "IsSelected" binded to the checkbox which is in the Assignment model.

The IsSelected property is triggering when I click on the checkbox. But "ObservableCollection ListActivities" is not triggering which is ViewModel when checkbox clicked.

How ObservableCollection ListActivities will trigger when clicks on the checkbox.

Please Help me.

Model:

public class Assignment : NotifyPropertyChanged
    {
      [JsonIgnore]
        private bool _isSelected;
        public bool IsSelected
        {
            get => _isSelected;
            set
            {
                _isSelected = value;
                RaisePropertyChanged(nameof(IsSelected));
            }
        }
    }

ViewModel:

    private ObservableCollection<Assignment> _listActivities;
    public ObservableCollection<Assignment> ListActivities
    {
        get => _listActivities;
        set
        {
            RaiseAndSetIfChanged(ref _listActivities, value);
            RaisePropertyChanged(nameof(ListActivities));
        }
    }

XAML:

<ListView x:Name="ClusteredActivities"
                      ItemsSource="{Binding ListActivities, Mode=TwoWay}"
                      Margin="20"
                      SeparatorVisibility="None"
                      HorizontalOptions="Center"> 
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout Orientation="Horizontal" Padding="0">
                                <CheckBox 
                                    HorizontalOptions="Start"
                                    VerticalOptions="Center"
                                    Color="Black"
                                    IsChecked="{Binding IsSelected, Mode=TwoWay}">
                                </CheckBox>
                                <Label Style="{DynamicResource ItalicLabel}">
                                    <Label.FormattedText>
                                        <FormattedString>
                                            <FormattedString.Spans>
                                                <Span Text="{Binding ParentIncident.ErpSrNumber}" />
                                                <Span Text=", " />
                                                <Span Text="{Binding ActivityId}" />
                                                <Span Text=", " />
                                                <Span Text="{Binding ActivityType}"/>
                                            </FormattedString.Spans>
                                        </FormattedString>
                                     </Label.FormattedText>
                                </Label>
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
Priyanka
  • 138
  • 2
  • 15

1 Answers1

0

From document ObservableCollection Class,we know that ObservableCollection<T> Class

Represents a dynamic data collection that provides notifications when items get added or removed, or when the whole list is refreshed.

So, we can receive notification once items get added or removed, or when the whole list is refreshed. On the other side, changing one property in the item will not trigger notification for ListActivities .

How ObservableCollection ListActivities will trigger when clicks on the checkbox.

If we want to trigger ListActivities ,we can add relative code in the setter method.

private bool _isSelected;
    public bool IsSelected
    {
        get => _isSelected;
        set
        {
            _isSelected = value;
            RaisePropertyChanged(nameof(IsSelected));

             // add relative code here for `ListActivities`  
        }
    }
Jessie Zhang -MSFT
  • 9,830
  • 1
  • 7
  • 19