首页 > 解决方案 > 如何获取viewCell中按钮的id

问题描述

我在 viewCell 中有一个按钮,并且在此标签文本 =“{Binding statusDescr}”中显示了两个已付费和已取消状态,当标签上的状态已支付时,按钮应该出现,而当它被取消时,按钮不应该出现出现。我的问题是我无法获取视图单元内按钮的 ID,以使其在标签上的状态已支付时可见,而在取消时不可见

<ListView x:Name="Lista"  
          SeparatorColor="Gray" 
          SeparatorVisibility="Default" 
          HasUnevenRows="True" 
          VerticalOptions="FillAndExpand" >
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout>
                    <StackLayout Orientation="Horizontal" >
                        <StackLayout Orientation="Horizontal" 
                                     HorizontalOptions="FillAndExpand">
                            <StackLayout>
                                <Label Text="{Bindenter code hereing entityName}" 
                                       TextColor="White" 
                                       Font="14"/>
                                <Label Text="{Binding cmPaymentDate}" 
                                       TextColor="White" 
                                       Font="14"/>
                                <Label Text="{Binding statusDescr}" 
                                       TextColor="White"
                                       Font="14"/>
                            </StackLayout>
                            <StackLayout HorizontalOptions="EndAndExpand">
                                <Button x:Name="cmdOpen" 
                                        Text="Open pdf" />
                                <Label Text="{Binding paymentAmount}" 
                                       TextColor="White"
                                       Font="14" 
                                       HorizontalOptions="End" />
                                <Label Text="{Binding paymentNumber}" 
                                       TextColor="White" 
                                       Font="10" 
                                       HorizontalOptions="End" />
                            </StackLayout>
                        </StackLayout>
                    </StackLayout>
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

标签: cxamarinxamarin.forms

解决方案


听起来像是将IsVisible按钮的属性绑定到付费状态的好地方:

<Button x:Name="cmdOpen" 
        IsVisible="{Binding paidState}" 
        Text="Open pdf" />

请注意,这仅在paidState 属性为布尔值时才有效。如果您只是使用类似双精度的东西来存储剩余金额,则需要使用转换器将值更改为布尔值。你的 XAML:

<ContentPage.Resources>
    <ResourceDictionary>
        <local:DoubleToBoolConverter x:Key="doubleToBool" />
    </ResourceDictionary>
</ContentPage.Resources>

<!--your code --->

<Button x:Name="cmdOpen" 
        IsVisible="{Binding amountRemaining, Converter={StaticResource doubleToBool}}" 
        Text="Open pdf" />

然后是转换器:

public class DoubleToBoolConverter : IValueConverter
{
    public object Convert(
               object value, 
               Type targetType, 
               object parameter, 
               CultureInfo culture)
    {
        return (double)value == 0;
    }

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

推荐阅读