1

Tengo un ListView en cual contiene varios controles Switch, le he colocado un BindingContext, pero al hacer Tap en el Switch en tiempo de ejecución, este cambia a true en todas las filas de la lista y lo que requiero es que solo cambie en la fila seleccionada.

Este es mi código XAML

    
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
             x:Class="MyApp.Views.CalificarPage"
             xmlns:local="clr-namespace:MyApp.Models"
             xmlns:viewModels="clr-namespace:MyApp.ViewModels"
             BindingContext="{Binding Main, Source={StaticResource Locator}}"
             BackgroundColor="White"
             Style="{DynamicResource NavigationPage}">
    <ContentPage.Resources>
        <ResourceDictionary>
            <local:StringToBoolConverter x:Key="stringToBool" />
        </ResourceDictionary>
    </ContentPage.Resources>
    <ContentPage.Content>
        <StackLayout BindingContext="{Binding Calificar}"
                     x:Name="Calificar">
            <StackLayout.Padding>
                <OnPlatform x:TypeArguments="Thickness">
                    <On Platform="Android" Value="0,0,0,0" />
                    <On Platform="iOS" Value="0,0,0,0" />
                </OnPlatform>
            </StackLayout.Padding>
            <ListView
                ItemsSource="{Binding MiLista}"
                HasUnevenRows="True">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <ViewCell.View>
                                <Grid BackgroundColor="White">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="*"/>
                                        <RowDefinition Height="*"/>
                                    </Grid.RowDefinitions>
                                    <StackLayout Grid.Row="0" Grid.ColumnSpan="4">
                                        <Grid>
                                            <BoxView
                                            BackgroundColor="#132F52">
                                            </BoxView>
                                            <Label
                                            FontAttributes="Bold"
                                            HorizontalOptions="StartAndExpand"
                                            Margin="5,0,0,0"
                                            Text="{Binding Usuario}"
                                            TextColor="White"
                                            VerticalOptions="CenterAndExpand">
                                            </Label>
                                        </Grid>
                                    </StackLayout>
                                    <StackLayout Grid.Row="1" Grid.ColumnSpan="4">
                                        <Grid>
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="*"/>
                                                <ColumnDefinition Width="*"/>
                                                <ColumnDefinition Width="*"/>
                                                <ColumnDefinition Width="*"/>
                                            </Grid.ColumnDefinitions>
                                            <StackLayout Grid.Column="0">
                                                <Switch
                                                    IsToggled="{Binding Path=BindingContext.Lunes, Source={x:Reference Name=Calificar}, Mode=TwoWay}"
                                                    VerticalOptions="CenterAndExpand"
                                                    HorizontalOptions="CenterAndExpand">
                                                </Switch>
                                            </StackLayout>
                                            <StackLayout Grid.Column="1">
                                                <Switch
                                                    IsToggled="{Binding Path=BindingContext.Martes, Source={x:Reference Name=Calificar}, Mode=TwoWay}"
                                                    VerticalOptions="CenterAndExpand"
                                                    HorizontalOptions="CenterAndExpand">
                                                </Switch>
                                            </StackLayout>
                                            <StackLayout Grid.Column="2">
                                                <Switch
                                                    IsToggled="{Binding Path=BindingContext.Miercoles, Source={x:Reference Name=Calificar}, Mode=TwoWay}"
                                                    VerticalOptions="CenterAndExpand"
                                                    HorizontalOptions="CenterAndExpand">
                                                </Switch>
                                            </StackLayout>
                                            <StackLayout Grid.Column="3">
                                                <Switch
                                                    IsToggled="{Binding Path=BindingContext.Jueves, Source={x:Reference Name=Calificar}, Mode=TwoWay}"
                                                    VerticalOptions="CenterAndExpand"
                                                    HorizontalOptions="CenterAndExpand">
                                                </Switch>
                                            </StackLayout>
                                        </Grid>
                                    </StackLayout>
                                </Grid>
                            </ViewCell.View>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
            <StackLayout>
                <Button
                    Style="{StaticResource FollowButton}"
                    Command="{Binding EnviarCommand}"
                    Margin="40,20,40,20"
                    BorderRadius="25"
                    HeightRequest="50"
                    Text="ENVIAR"
                    TextColor="White"
                    VerticalOptions="CenterAndExpand"
                    BackgroundColor="#132F52">
                </Button>
            </StackLayout>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

1
  • A los Switch le estás pasando el BindingContext del StackLayout Calificar, por lo que cambiarás el estado de todos los elementos que hacen el binding ahí. Necesitarás cambiar el BindingContext y que el modelo que le pasas a cada Cell tenga las propiedades que necesitas. No olvides implementar el INotifyPropertyChanged en el modelo que pasas al ViewCell. el 15 ago. 2018 a las 11:23

0

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

Examina otras preguntas con la etiqueta o formula tu propia pregunta.