c - 如何获取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>
解决方案
听起来像是将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();
}
}
推荐阅读
- xtermjs - 如何在 xtermjs 中设置缓冲区的大小?
- python - 从不同的脚本登录到同一个文件?
- python - 网络抓取时如何维护与同一主机的多个会话?
- amazon-web-services - 如何使用 Amazon Cognito Identity Js 在 Aws Cognito 中实施 RBAC
- java - 如何将 Java 字符串解析为 YAML 中的文字块
- php - PHP mysql while循环在foreach循环中重复
- javascript - 将新对象添加到多维数组
- javascript - 仅在溢出时才在悬停时选择字幕
- r - 将多维数组读入R
- typescript - 向 Parse.User 扩展/添加方法