如何显示/隐藏列表视图中的按钮

时间:2018-09-21 13:00:04

标签: c# xamarin xamarin.forms

我的列表视图中填充了来自API的数据,但是在我的列表视图中,我有一个“ statusDescr”标签,其中显示了两种状态(已付款/已取消),当标签上的状态已付款时,按钮就会出现,不显示。

我已经将IsVisible属性放在按钮上,但是我仍然有很多问题

<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>

代码

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();
        }

模型

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; }
    }
}

2 个答案:

答案 0 :(得分:1)

我将在做两个假设时回答这个问题:

  1. 您要问的实际问题是“如何将按钮的IsVisible属性绑定到模型的属性?”
  2. 您对属性的绑定实际上正在起作用。我提出这一问题的唯一原因是我看不到INotifyPropertyChanged在任何地方都被使用。

基于上述假设,我建议不要使用Value Converter来设置IsVisible属性,而不是为模型添加额外的属性来处理视图状态。

转换器将类似于:

public class StringToBoolConverter : IValueConverter
{
    public object Convert(
           object value, 
           Type targetType, 
           object parameter, 
           CultureInfo culture)
    {
        return (string)value == "paid";
    }

    public object ConvertBack(
           object value, 
           Type targetType, 
           object parameter, 
           CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

您的视图将如下所示:

<ContentPage.Resources>
    <ResourceDictionary>
        <local:StringToBoolConverter x:Key="stringToBool" />
    </ResourceDictionary>
</ContentPage.Resources>

<!--your code --->

<Button  Text="Abrir" 
         IsVisible="{Binding statusDescr, Converter={StaticResource stringToBool}}"  
         BackgroundColor="#1C97D5" 
         TextColor="White" />

然后可以从IsVisible模型中删除Payment属性。

我会注意到,如果您对API拥有任何控制权,则最好是如果只有两个状态,则将其发回布尔值;如果有两个以上的状态,则将其发送回Enum更好。字符串容易折断。

答案 1 :(得分:0)

使用DataTrigger for Buttom使其可见或不可见 使用以下代码

<ListView x:Name="lstView" 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"/>
                            <Label x:Name="lblestado" Text="{Binding statusDescr}" TextColor="White" Font="14"/>
                        </StackLayout>
                        <StackLayout HorizontalOptions="EndAndExpand">
                            <Button  Text="Abrir" BackgroundColor="#1C97D5" TextColor="White">
                                <Button.Triggers>
                                    <DataTrigger TargetType="Button"  Binding="{Binding statusDescr" Value="canceled">
                                        <Setter Property="IsVisible" Value="False"/>
                                    </DataTrigger>
                                    <DataTrigger TargetType="Button"  Binding="{Binding statusDescr" Value="paid">
                                        <Setter Property="IsVisible" Value="True"/>
                                    </DataTrigger>
                                </Button.Triggers>
                            </Button>
                            <Label Text="{Binding paymentAmount}" TextColor="White" Font="14" HorizontalOptions="End" />
                        </StackLayout>
                    </StackLayout>
                </StackLayout>
            </StackLayout>
        </ViewCell>
    </DataTemplate>
</ListView.ItemTemplate>