Listview click by constructor call but not by button

Asked

Viewed 57 times

0

I have a listview in my app and created in my Viewmodel a method Carregarlistview();

When I call it by the constructor of my Viewmodel, an Observablecollection list is loaded and Binding is given in my view.

When I call the Load Stview() method by a command from a button the Load Stview() method is called and the list is loaded, but does not give Binding in my view.

Does anyone know why?

This is my listview

    <ListView Grid.Row="2"
                      Grid.Column="1"
                      Grid.ColumnSpan="3" 
                      ItemsSource="{Binding ListaIntervalos }" 
                      HasUnevenRows="true">

                <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Grid> 
                            <Grid.RowDefinitions>
                                <RowDefinition Height="20"/>
                            </Grid.RowDefinitions>

                            <Grid.ColumnDefinitions>
                                <ColumnDefinition />
                                <ColumnDefinition />
                            </Grid.ColumnDefinitions>

                            <Label Grid.Row="1" Grid.Column="0" Text="{Binding De}"  FontSize="20" VerticalOptions="Start" TextColor="Black"/>
                            <Label Grid.Row="1" Grid.Column="1" Text="{Binding Ate}"  FontSize="20" VerticalOptions="Start" TextColor="Black"/>

                        </Grid>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

And in my View Model it’s like this

    public ObservableCollection<Intervalo> ListaIntervalos { get; private set; } = new ObservableCollection<Intervalo>();

    //Construtor
    public VMBuscarSalto()
    {
        // Aqui funciona
        this.CarregaIntervalos();
    }

    public Command BuscaSaltosCommand
    {
        get
        {
            return new Command(() => ExecuteBuscaSaltosCommand());
        }
    }

    private void ExecuteBuscaSaltosCommand()
    { 
        // Aqui a lista é carregada mas não dá o bind no listview
        this.CarregaIntervalos();
    }

1 answer

0

I believe that the problem reported was not something that I did wrong, but it should be a bug in Xamarin’s Listview component, which between us, is full of flaws regarding the use of its features using MVVM.

I solved my problem using Code Behind, which is simply loading the Itemssource property from listview into XAML

    <ListView Grid.Row="2"
                      Grid.Column="1"
                      Grid.ColumnSpan="3"  
                      x:Name="GridDeIntervalos">

and in code Behind

     List<Intervalo> listIntervaloModel = new List<Intervalo>();


            for (int i = 0; i < 10; i++)
            {
                listIntervaloModel.Add(new Intervalo
                {
                    De = "teste " + i,
                    Ate = "teste " + i
                });
            }

            GridDeIntervalos.ItemsSource = listIntervaloModel;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.