RepeaterView control click command

IvanTelesIvanTeles USMember ✭✭
edited October 2017 in Xamarin.Forms
<controls:RepeaterView  x:TypeArguments="Model:Farmacia" ItemsSource="{Binding Unidades}" TemplateSelector="">

                <controls:RepeaterView.ItemTemplate>
                    <DataTemplate>
                        <StackLayout  Orientation="Vertical" VerticalOptions="Center" Padding="0,5,3,0">
                            <StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand" Margin="0">
                                <Image Source="{Binding Foto}" WidthRequest="70" HeightRequest="95" Margin="5,0,0,0" />
                                <StackLayout Orientation="Vertical" VerticalOptions="StartAndExpand" HorizontalOptions="FillAndExpand" Padding="0,5,0,5" Spacing="0">
                                    <Label Text="{Binding Nome}" Margin="2,5,0,0" FontAttributes="Bold" FontSize="16" />
                                    <Label Text="{Binding Endereco}" Margin="2,5,0,0" TextColor="Silver" FontSize="14" />
                                    <Label Text="{Binding Telefone}" Margin="2,0,0,5" TextColor="Silver" FontSize="12" />
                                </StackLayout>
                                <StackLayout Orientation="Vertical" VerticalOptions="EndAndExpand" HorizontalOptions="FillAndExpand" Padding="0,5,0,5" Spacing="0">
                                    <Label Text="{Binding Valor}" Margin="2,5,0,0" FontAttributes="Bold" FontSize="16" HorizontalTextAlignment="End" />

                                    <StackLayout Orientation="Horizontal" VerticalOptions="EndAndExpand" HorizontalOptions="FillAndExpand" Padding="5,5,0,5" Spacing="0">
                                        <StackLayout Orientation="Vertical" VerticalOptions="CenterAndExpand" Padding="0">
                                            <iconize:IconButton Text="md-place" Command="{Binding ShowMapaCommand}" Margin="0" WidthRequest="40"  HorizontalOptions="Start" BackgroundColor="#00a650" TextColor="White"></iconize:IconButton>
                                        </StackLayout>
                                        <StackLayout Orientation="Vertical" VerticalOptions="CenterAndExpand" Padding="0">
                                            <iconize:IconButton Text="md-add-shopping-cart" Command="{Binding AddCartCommand}" WidthRequest="40" Margin="0" HorizontalOptions="End" BackgroundColor="#00a650" TextColor="White"></iconize:IconButton>
                                        </StackLayout>
                                    </StackLayout>
                                </StackLayout>
                            </StackLayout>
                            <controls:Separator Color="Silver" SpacingAfter="2" SpacingBefore="2" StrokeType="Solid" Thickness="1"></controls:Separator>
                        </StackLayout>
                    </DataTemplate>
                </controls:RepeaterView.ItemTemplate>
            </controls:RepeaterView>

Why do not the events on the two buttons work? Do you have to do something specific to work?

Posts

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    My guess is BindingContext.
    This is a template for Items out of a collection. All the properties are on the ELEMENT of the COLLECTION, right? Like the text and so on.

    That would include the command.
    <iconize:IconButton Text="md-place" Command="{Binding ShowMapaCommand}"
    In this binding the command and the text are expected to come off the same object.

    Is the command on the element of the collection? or is it someplace higher up, like on the ViewModel?

  • IvanTelesIvanTeles USMember ✭✭

    My Page is linked to a ViewModel, and in the viewmodel there is the command.

        public ICommand ShowMapaCommand { get; }
        public ProdutoViewModel(Produto prod)
                {
                    Produto = prod;
                    ShowMapaCommand = new Command(ExeShowMapaCommand);
                }
    
    private void ExeShowMapaCommand()
            {
                PushAsync<MapaPage>();
            }
    
  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    and in the viewmodel there is the command.

    Okay. Like I said... Your command is in the VIewModel but you're template would be looking at the element in your collection just like it does for all the other properties.

  • IvanTelesIvanTeles USMember ✭✭

    Clint StLaurent, Thank you, really. I created the command inside my object and it worked perfectly!

  • UmarHassanUmarHassan Member

    Can You Share how you created the command inside your object

  • IvanTelesIvanTeles USMember ✭✭

    follw code

    public class Farmacia
        {
            public int Id { get; set; }
            public string Nome { get; set; }
            public string Endereco { get; set; }
            public string Telefone { get; set; }
            public double Latitude { get; set; }
            public double Longitude { get; set; }
            public string Foto { get; set; }
            public string Valor { get; set; }
            public Produto Produto { get; set; }
            public Command<Produto> ShowMapaCommand { get; }
            public Command<Farmacia> ComprarCommand { get; }
            public Farmacia(Produto p)
            {
                Produto = p;
                ShowMapaCommand = new Command<Produto>(ExeShowMapaCommand);
                ComprarCommand = new Command<Farmacia>(ExeComprarCommand);
            }
            private void ExeShowMapaCommand(Produto obj)
            {
                PushAsync<MapaPage>(obj);
            }
            private void ExeComprarCommand(Farmacia objec)
            {
                Global.Instancia.Compras.Add(new Compra{ Drogaria = objec.Nome, Valor = objec.Valor, Produto = Produto.Nome });
                PushAsync<CompraPage>();
            }
        }
    
Sign In or Register to comment.