2
respostas

Binding de um label não está funcionando ao clicar no botão atualizar

Possuo um botão que quando clicado, pega o valor que foi digitado pelo usuário e realiza um cálculo. Após isso gostaria de apresentar o resultado do cálculo em uma label. Porém isso não ocorre e o label fica vazio.

Detalhes: O label está inserido dentro de um ListView, acredito que isso seja a causa ou uma das, pois se deixo o label fora do list ele é atualizado normalmente com o resultado do cálculo.

XAML:

<ContentPage.BindingContext> vm:ResumovendaViewModel </ContentPage.BindingContext> <ContentPage.Resources> <converters:Converters x:Key="negativo"> </ContentPage.Resources> <ContentPage.Content> <ListView.ItemTemplate > <ViewCell.View>

                                <!--Imagem do Produto-->
                                <StackLayout Orientation="Vertical">
                                    <Image Source="{Binding LocalImagemProduto, Mode=TwoWay}" Aspect="AspectFit" WidthRequest="15" Margin="0,0,15,0" />
                                </StackLayout>


                                <!--Nome e valor do Produto-->
                                <StackLayout Orientation="Vertical" WidthRequest="80" >
                                    <Label x:Name="NomeProduto" Text="{Binding NomeProduto}" FontSize="15" VerticalTextAlignment="Center" TextColor="#006699"></Label>
                                    <Label x:Name="ValorProduto" Text="{Binding ValorProduto, StringFormat='R$ {0}'}" FontSize="10" FontAttributes="Bold" VerticalTextAlignment="Center"  TextColor="#f78f2b"></Label>
                                </StackLayout>

                                <!--Quantidade solicitada do Produto-->
                                <StackLayout Orientation="Vertical" WidthRequest="50">
                                    <!--<Stepper x:Name="StpQuantidade" HorizontalOptions="CenterAndExpand" Minimum="0" Increment="1" />-->
                                    <Entry x:Name="TxtQtde" Placeholder="{Binding ResumoVendaModel.QtdProd}"  Text="{Binding ResumoVendaModel.QtdProd}"   Keyboard="Numeric" ></Entry>
                                </StackLayout>


                                <StackLayout Orientation="Vertical">
                                    <!--<Label x:Name="txtquantidade" Text="{Binding Source={x:Reference Qtde}, Path=Text, StringFormat='QTDE: {0}'}"  FontSize="12" VerticalTextAlignment="Center"  />-->
                                    <!--<ImageButton x:Name="BtnCarrinho" Source="carrinho_compras.jpeg" Aspect="AspectFit" HeightRequest="35" WidthRequest="35"  
                                                Clicked="BtnCarrinho_Clicked" CommandParameter="{Binding .}"  BackgroundColor="Transparent" />-->
                                    <ImageButton x:Name="BtnCarrinho" Source="carrinho_compras.jpeg" Aspect="AspectFit" HeightRequest="35" WidthRequest="35" BindingContext="{Binding Source={x:Reference LstProdutos}, Path=BindingContext}"
                                                 Command="{Binding CalcularCommand}" CommandParameter="{Binding Source={x:Reference Item},Path=BindingContext}" BackgroundColor="Transparent" />
                                </StackLayout>

                                <StackLayout>
                                    <Label x:Name="txtQtdeTotal" Text="{Binding Source={x:Reference TxtQtde}, Path=Text }"   FontSize="12" VerticalTextAlignment="Center"  />
                                    <Label x:Name="txtvalor" Text="{Binding Vlr_Total, Mode=TwoWay,StringFormat='Valor Total: {0:F0}'}"   FontSize="12" VerticalTextAlignment="Center"  />
                                </StackLayout>


                            </StackLayout>
                        </ViewCell.View>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

        <StackLayout Orientation="Horizontal">
            <Label x:Name="lblTotalProdutos" Text=""  FontSize="12" VerticalTextAlignment="Center" HorizontalOptions="FillAndExpand"  />

        </StackLayout>
2 respostas

View Model:

[XamlCompilation(XamlCompilationOptions.Compile)]
public class ResumovendaViewModel : BaseViewModel
{
    public ObservableCollection<Produtos> _prod;
    public Produtos produto { get; set; }

    public ObservableCollection<Produtos> Produto { get { return _prod; } set { _prod = value; OnPropertyChanged(nameof(Produto)); } }


    public ICommand ActionCalcular { get; set; }

    public ResumovendaViewModel()
    {
        this.Produto = new ListagemProdutos().Produto;
        ProdutoObjeto = new Produtos();
    }

    private Produtos _produto;
    public Produtos ProdutoObjeto {
        get {
            return _produto;
        }

        set {
            _produto = value; OnPropertyChanged();
        }
    }

    private string _teste;
    public string Teste { get { return _teste; } set { _teste = value; OnPropertyChanged("Teste"); } }


    private string _NomeProduto;
    public string NomeProduto { get { return _NomeProduto; } set { _NomeProduto = value; OnPropertyChanged("NomeProduto"); } }

    private string _ValorProduto;
    public string ValorProduto { get { return _ValorProduto; } set { _ValorProduto = value; OnPropertyChanged("ValorProduto"); } }


     private string _Vlr_Total;
     public string Vlr_Total {
         get { return _Vlr_Total; }
         set { _Vlr_Total = value; OnPropertyChanged("Vlr_Total");
         }
     }

    private int _QtdProd;
    public int QtdProd { get { return _QtdProd; } set { _QtdProd = value; OnPropertyChanged("QtdProd"); } }



    public void calcular(ResumoVendaModel.Produtos item)
    {
        var TotalProd = item.QtdProd * Convert.ToDecimal(item.ValorProduto);

        this.Vlr_Total = TotalProd.ToString();
        this.Teste = TotalProd.ToString();

        OnPropertyChanged(nameof(Produto));
        OnPropertyChanged(nameof(Vlr_Total));

    }

    public Command CalcularCommand {
        get {
            return new Command((item)=>{
                calcular((ResumoVendaModel.Produtos) item);
            });
        }
    }
}

Estou enfrentando o mesmo problema ! Não consigo acessar a variavel que está dentro da ViewList

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software