samedi 16 juillet 2022

Checkbox doesn't update attached property in ObservableCollection of objects

Model

    public class ThemeItem:BaseItem
    {
        [Ignore]
        public bool IsSelected { get; set; }
        public string Name { get; set; }
        public int NumberOfWords { get; set; }
    }

    public class BaseItem
    {
        [PrimaryKey, AutoIncrement]
        public int Id { get; set; } 
    }

View

<CollectionView ItemsSource="{Binding Themes}" SelectionMode="None">
                    <CollectionView.ItemTemplate>
                        <DataTemplate x:DataType="m:ThemeItem">
                            <Grid Padding="5"
                                  xct:TouchEffect.Command="{Binding Source={RelativeSource AncestorType={x:Type vm:ThemeItemsViewModel}},Path=ShowDetailsCommand}"
                                  xct:TouchEffect.CommandParameter="{Binding .}"
                                  xct:TouchEffect.LongPressCommand="{Binding Source={RelativeSource AncestorType={x:Type vm:ThemeItemsViewModel}},Path=SelectionCommand}"
                                  xct:TouchEffect.LongPressCommandParameter="{Binding .}">
                                <Frame BorderColor="LightGray" CornerRadius="5" Padding="15">
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="auto"/>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="auto"/>
                                        </Grid.ColumnDefinitions>
                                        <CheckBox Grid.Column="0" IsChecked="{Binding IsSelected,Mode=TwoWay}" 
                                                  IsVisible="{Binding Source={RelativeSource AncestorType={x:Type vm:ThemeItemsViewModel}},Path=IsSelection}"/>
                                        <Label Grid.Column="1" Text="{Binding Name}" FontSize="Large"/>
                                    </Grid>
                                </Frame>
                            </Grid>
                        </DataTemplate>
                    </CollectionView.ItemTemplate>
                </CollectionView>

ObservableCollection

public ObservableCollection<ThemeItem> Themes { get; set; }

Command

        public AsyncCommand DeleteSelectedCommand { get; set; }
        private async Task OnDeleteSelectedCommandExecuted()
        {
            IsBusy = true;
            bool delete = await App.Current.MainPage.DisplayAlert("Delete", "Are you sure you want to delete selected themes and words it contains?", "Yes", "No");
            if(true)
            {
                foreach (ThemeItem t in Themes.Where(t=>t.IsSelected))
                {
                    await _themeDataService.DeleteItemAsync(t.Id);
                }
                await LoadList();
            }
            IsBusy = false;
        }

I'm building dictionary app with xamarin.forms. I have a list of themes, each theme has a checkbox that is binded to "IsSelected" property. After I select theme I want to push button and delete it with DeleteSelectedCommand command, but when I put a tick in checkbox, property "IsSelected" doesn't update though I use TwoWay mode. [Ignore] tag is for SqLite.




Aucun commentaire:

Enregistrer un commentaire