How to detect click/tap on switch and get value of entry fields in ViewModel?.

DevMaranDevMaran Member ✭✭
edited August 2018 in Xamarin.Forms

I have a student Attendance page in which I have a list of students with a switch and entry field for each student

    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:filters="clr-namespace::testapp" xmlns:ff="clr-namespace:FFImageLoading.Forms;assembly=FFImageLoading.Forms" xmlns:ffTransformations="clr-namespace:FFImageLoading.Transformations;assembly=FFImageLoading.Transformations" x:Class="testapp.StudenceAttendancePage" xmlns:custom="clr-namespace:testapp" xmlns:local="clr-namespace:testapp" Title="Studennce Attendance" >
    <StackLayout>
        <ListView x:Name="listViewLocation" ItemsSource="{Binding ClassAttendance}" HasUnevenRows="true" SelectedItem="{Binding SelectedItem}">
                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <ViewCell>
                                        <ViewCell.View>
                                            <Frame Margin="20,2,20,3">
                                                <StackLayout HorizontalOptions="Center">
                                                    <ff:CachedImage Source="{Binding ProfilePicture}" ErrorPlaceholder="NoImagePlaceHolder.png" 
                                                                    LoadingPlaceholder="NoImagePlaceHolder.png" HeightRequest="150" WidthRequest="10">
                                                        <ff:CachedImage.Transformations>
                                                            <ffTransformations:RoundedTransformation Radius="240" />
                                                        </ff:CachedImage.Transformations>
                                                    </ff:CachedImage>
                                                    <StackLayout Orientation="Horizontal">
                                                        <!--<cs:CustomSwitch x:Name="customSwitch" SwitchBGColor="Blue" SwitchThumbColor="{StaticResource primary}" IsToggled="{Binding IsChecked}" HorizontalOptions="EndAndExpand" VerticalOptions="CenterAndExpand" />-->
                                                        <Switch IsToggled="{Binding Present}" IsEnabled="true" VerticalOptions="CenterAndExpand" HorizontalOptions="EndAndExpand" />
                                                        <custom:CustomEntry BorderWidth="1" CornerRadius="5" IsCurvedCornersEnabled="True" BorderColor="{StaticResource primary}" FontSize="17" HeightRequest="50" TextColor="Black" FontAttributes="Bold" WidthRequest="100" />
                                                    </StackLayout>
                                                </StackLayout>
                                            </Frame>
                                        </ViewCell.View>
                                    </ViewCell>
                                </DataTemplate>
                            </ListView.ItemTemplate>
                            <ListView.Footer>
                                <Label />
                            </ListView.Footer>
                        </ListView>
        </StackLayout>
    </ContentPage>

Model:

            public class ClassAttendance
                {
                    public int StudentId { get; set; }
                    public string ProfilePicture { get; set; }
                public bool Present { get; set; }
                    public string Reason { get; set; }
                }

I have tried the solution at https://forums.xamarin.com/discussion/73470/detecting-tap-on-label-inside-viewcell-inside-listview
but I am getting an error in the ViewModel:

I want to get the value of entry field when switching event occurs in the ViewModel.

Best Answer

Answers

Sign In or Register to comment.