Search code examples
c#xamlcheckboxxamarinxlabs

How to perform action when CheckBox list view item is selected?


I have a Xamarin.Forms project where I am using Xlabs to implement checkboxes. I can get them to appear which is great. I want to be able to do this:

void OnSelection(object sender, SelectedItemChangedEventArgs e)
    {
        if (e.SelectedItem == null)
            return;
        var selectedStudent = ((ListView)sender).SelectedItem as Student;

        // set time of student checkin
    }

But with my check boxes. I want to be able to handle checkbox selection as it happens. From what I've seen online I can set a new boolean property of my student object selected, but that simply doesn't work with my project. Here is my current approach:

    void OnSelection(object sender, EventArgs e)
    {
        CheckBox isCheckedOrNot = (CheckBox)sender;
        var name = isCheckedOrNot.DefaultText;

        //not sure what to do here?
        var student = ((CheckBox)sender)...

        // set time of student checkin
    }

EDIT: checkbox declared as so:

<ViewCell>
    <ViewCell.View>
        <controls:CheckBox DefaultText="{Binding complete_name}"  TextColor="Black" CheckedChanged="OnSelection" Checked="{Binding selected}"/>
    </ViewCell.View>
</ViewCell>

Solution

  • I am very new to xaml/c# so I apologize for asking this bad question but it is as simple as saying:

        void OnSelection(object sender, EventArgs e)
        {
            CheckBox isCheckedOrNot = (CheckBox)sender;
            var selectedStudent = isCheckedOrNot.BindingContext as Student;
            ...
        }
    

    to get the item that you checked.