How to show / hide a button inside a listview

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

I already put the IsVisible property 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; }
}
    
asked by anonymous 21.09.2018 / 12:11

1 answer

0

Try to pass an ObservableCollection instead of passing a list of objects directly to the Source of the control. I'm not sure if that's the correct answer, but it's worth a try. Here's an example 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;
    }
}
    
22.09.2018 / 16:50