如何在viewCell中获取按钮的id

问题描述 投票:0回答:1

我在viewCell中有一个按钮,我有两个付费和取消状态出现在这个Label Text =“{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>
c xamarin xamarin.forms
1个回答
4
投票

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

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

请注意,这仅在paidState属性为bool时才有效。如果你只是使用像double这样的东西存储剩余的数量,你需要使用converter将值更改为bool。你的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();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.