0
votes

I have this listview and in the listview I have an ItemTemplate with a DataTemplate and a ViewCell in which I have a checkbox named "box1". I want to make it stay checked when i switch pages, but i can't acess it via name because it is in a DataTemplate and in a ViewCell. I have tried to name all the controls down to the checkbox and get access to it like that, but it does not seem to work...

This is my xaml:

<ListView SeparatorVisibility="None"
                  BackgroundColor="Transparent"
                  VerticalOptions="Center"
                  x:Name="listView"
                  HasUnevenRows="True"
                  >
            <ListView.ItemTemplate>
                <DataTemplate x:DataType="model:Meal"
                              x:Name="mydt"
                              >
                    <ViewCell
                        x:Name="myvc"
                        >

                        <Grid BackgroundColor="Transparent"
                              x:Name="mygrid"
                              >
                            <Frame BackgroundColor="Transparent"
                                   CornerRadius="20"
                                   x:Name="myframe"
                                   >
                                <StackLayout Orientation="Horizontal"
                                             >
                                    <Image Source="meal.png" WidthRequest="59" Margin="0, 0, 15, 0"/>
                                    <StackLayout Orientation="Vertical" WidthRequest="300">
                                    <Label VerticalOptions="Start"
                                       FontSize="20"
                                       Text="{Binding Name}"
                                       FontAttributes="Bold"/>
                                    <Label VerticalOptions="Start"
                                       FontSize="15"
                                       Text="{Binding Ingredients}"/>
                                        <StackLayout Orientation="Horizontal">
                                            <Label VerticalOptions="Start"
                                               FontSize="15"
                                               Text="{Binding Calories}"
                                               TextColor="OrangeRed"/>
                                            <Label Text="kcal" 
                                                   FontSize="15"
                                                   TextColor="OrangeRed"/>
                                        </StackLayout>

                                    </StackLayout>
                                    <CheckBox 
                                       
                                        x:Name="box1"
                                          IsChecked="{Binding Checked}"
                                              Color="Green"
                                              Margin="60, 0, 0, 0"
                                              CheckedChanged="box1_CheckedChanged"
                                              BindingContext="{Binding ., Mode=TwoWay}"
                                              />
                                  

                                </StackLayout>
                            </Frame>
                        </Grid>
                            
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

This is my event handler from the Content Page in cs:

    private void box1_CheckedChanged(object sender, CheckedChangedEventArgs e)
    {           
        var meal = listView.SelectedItem as Meal;
        if (listView.SelectedItem != null)
        {
            if (e.Value == true)
            {
                long cal = long.Parse(meal.Calories);
                calories_consumed = calories_consumed + cal;
                ch = true;
            }
            else
            {
                long cal = long.Parse(meal.Calories);
                calories_consumed = calories_consumed - cal;
                ch = false;
            }
        }
        label_cal.Text = calories_consumed.ToString();
    } 
1
you need to bind the CheckBox checked property to some bool in your modelJason

1 Answers

0
votes

To be short you can just save the value in Preferences or make a variable in a model that is static and bind that values OnAppearing