Home > Enterprise >  Display a list of names based on checked boxes in other page - Xamarin Forms
Display a list of names based on checked boxes in other page - Xamarin Forms

Time:02-01

I have a view with a lot of labels and checkboxes, it is basically a list of questions for the user and if the user answers yes, they check the checkbox, otherwise they don't. I have another view that is related to this first view in which it should get and display which check boxes were checked. For example:

<Label Text="You have fever?" />
<CheckBox x:Name="fever" />

<Label Text="You have a running nose?" />
<CheckBox x:Name="running_nose" />

<Label Text="You are feeling dizzy?" />
<CheckBox x:Name="dizzy" />

And on the other page I want to display the values that were checked in a entry field. Let's say the user checked fever and dizzy. On the other page it would display something like this:

"You marked *fever*, *dizzy*. Is that right?"

How can I do this? I know I can use data binding. But not sure how to. In my actual app, I have around 10 fields of those and I need to display on the other page the was that were marked. I was also thinking in instead of displaying an entry with these values, I would created a card for each one of the checked boxes (my app is much more complex, the symptoms are just an example).

CodePudding user response:

you need to model your data

create a class that is something like this

public class Symptom
{
  public string Name { get; set; }
  public string Prompt { get; set; }
  public bool Selected { get; set; }
}

then create a list with data

List<Symptom> symptoms = new List<Symptom>();

symptoms.Add(new Sympton { Name = "fever", Prompt = "You have fever?" });
// repeat for others

then you can display this data in a ListView or CollectionView

<ListView x:Name="lstSymptoms">
<ListView.ItemTemplate>
    <DataTemplate>
        <ViewCell>
            <StackLayout>
                <Label Text="{Binding Prompt}" />
                <Checkbox Selected="{Binding Selected" />
            <StackLayout>
        </ViewCell>
    </DataTemplate>
</ListView.ItemTemplate>
<ListView>

then in the code behind, assign your model to the list

lstSymptoms.ItemSource = symptoms;

when the user is done selecting, you can use LINQ to create a list of just the selected symptoms, and pass that to the next page

  •  Tags:  
  • Related