How to show / hide a button inside a listview

Asked

Viewed 165 times

2

Man ListView is populated with data that comes from an API, but within my template I have a label statusDescr where it shows two states ("paid" / "canceled") when the status on the label is paid the button appears, and when not the button must be hidden.

I’ve already placed the property IsVisible on the button, but I still have many problems

<ListView x:Name="lstView" 
          SeparatorColor="#1C97D5" 
          SeparatorVisibility="Default" 
          HasUnevenRows="True" 
          VerticalOptions="FillAndExpand">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell StyleId="disclosure">
                <StackLayout>
                    <StackLayout Orientation="Horizontal" >
                        <StackLayout Orientation="Horizontal" 
                                     HorizontalOptions="FillAndExpand">
                            <StackLayout>
                                <Label Text="{Binding entityName}" 
                                       TextColor="White" 
                                       Font="14"/>
                                <Label Text="{Binding cmPaymentDate}" 
                                       TextColor="White" 
                                       Font="14"/>
                                <!--the label below is where the states will appear-->
                                <Label x:Name="lblestado" 
                                       Text="{Binding statusDescr}" 
                                       TextColor="White" 
                                       Font="14"/>
                            </StackLayout>
                            <StackLayout HorizontalOptions="EndAndExpand">
                                <!--This is the button that should be true / false-->
                                <Button Text="Abrir" 
                                        IsVisible="{Binding IsVisible}"  
                                        BackgroundColor="#1C97D5" 
                                        TextColor="White">
                                </Button>
                                <Label Text="{Binding paymentAmount}" 
                                TextColor="White" Font="14" HorizontalOptions="End" />
                            </StackLayout>
                        </StackLayout>
                    </StackLayout>
                </StackLayout>
           </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Code

try 
{
    string url = "payment/searchByDates/" + min + "/" + max + "/" + App.Nif + "/" + App.accountId;
    Service<Response<Payment>> servico = new Service<Response<Payment>>(url);

    var x = servico.GetByID(null).Result;

    if (x.GetType() == (typeof(Response<Payment>)))
    {
        var pay = (Response<Payment>)x;

            lstView.ItemsSource = pay.result;
            UserDialogs.Instance.HideLoading();

        //if (statusDescr == "Pago")
        //{
        //    lstView.ItemsSource = pay.result;
        //    UserDialogs.Instance.HideLoading();
        //    IsVisible = true;
        //}
        //else
        //{
        //    if (statusDescr == "Cancelado")
        //    {
        //        lstView.ItemsSource = pay.result;
        //        UserDialogs.Instance.HideLoading();
        //        IsVisible = false;
        //    }
        }
    }
    else
    {
        DisplayAlert("Não encontrado", "Não foi encontrado os dados solicitados", "OK");
    }
}
catch (Exception ex)
{
    DisplayAlert("Erro", ex.Message, "OK");
    UserDialogs.Instance.HideLoading();
}

Model

public class Payment
{
    public string cmPaymentDate { get; set; }
    public string entityName { get; set; }
    public string statusDescr { get; set; }
    public string paymentNumber { get; set; }
    public float paymentAmount { get; set; }
    public bool IsVisible { get; set; }
}

1 answer

0

Try instead of passing a list of objects directly to the Source control, pass an Observablecollection. I’m not sure if it’s the right answer, but it’s worth a try. Here’s an example of code.

public partial class MainDetail : ContentPage
{
    public ObservableCollection<Payment> Items { get; set; }

    public MainDetail()
    {
        List<Payment> LISTA_RESULTADO_DA_API = new List<Payment>();
        Items = new ObservableCollection<Payment>(LISTA_RESULTADO_DA_API);
        lstView.ItemsSource = Items;
    }
}

Browser other questions tagged

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