0
votes

I am working on Xamarin Forms project and in Visual Studio 2017. I have toggle button inside a list view I need to remove toggled event and add it again using code. I cannot get toggle name since it is inside a listview. Is there any way I can identify event fires from the code or from user action? XAML is below

<ListView x:Name="listData"  
            Grid.Row="7" Grid.Column="1" Grid.ColumnSpan="7"
            SeparatorColor="Transparent" BackgroundColor="Transparent" 
            HasUnevenRows="True" IsVisible="false">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout Orientation="Vertical">
                    <StackLayout Orientation="Vertical">
                        <Switch 
                            Scale="0.85"
                            WidthRequest="50"
                            HeightRequest="35"
                            IsToggled="{Binding IsCrewMemberSelected}"
                            VerticalOptions="Center"
                            HorizontalOptions="Start"
                            Toggled="Handle_IndividualToggled"
                            Margin="0,0,0,0"/>
                        <Label 
                            HeightRequest="35"
                            TextColor="#FFFFFF"
                            FontFamily="Open Sans"
                            FontSize="16"          
                            Text="{Binding CrewMemberName}"
                            VerticalOptions="Center"
                            HorizontalOptions="Start"
                            Margin="60,-35,0,0"/>
                    </StackLayout>
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
1

1 Answers

0
votes

Just bind the switch in the listView

<Switch IsToggled="{Binding isToggled, Mode=TwoWay}" ></Switch>

and in the Object of that list define a variable as:

public Problem(string ProblemID, string ProblemName,bool isToggled)
{
    this.ProblemID = ProblemID;
    this.ProblemName = ProblemName;
    this.isToggled = isToggled;
}

Since you bind the value, you can initialize the switch when you add the item to true and it will make the switch on. Default is false which will make the switch off.

After that, use a simple loop to get all items where isToggled == true.