![](/img/trans.png)
[英]Xamarin.ListView. How binding working in itemtemplate/datatemplate?
[英]Xamarin Forms Listview Datatemplate IsVisible Binding not working?
我有一个利用 DataTemplate 的 ListView。 在数据模板中,我有一些 StackLayouts。 ListView ItemsSource 设置为“Category”列表(Category 是我的类),其中一个 StackLayouts 的 IsVisible 属性绑定到 Category.IsVisible。 ClassId 也被绑定,所以在点击手势时,我找出哪个 Category 项目是 ListView 项目后面的项目并将其设置为 IsVisible。 但这不起作用,并且 StackLayout 仍然可见
<ListView
SeparatorColor="#888888"
SeparatorVisibility="Default"
BackgroundColor="White"
SelectionMode="None"
HasUnevenRows="True"
x:Name="mainListView">
<ListView.Header>
<ContentView
Padding="0,5">
<Label
Margin="0,0,0,15"
FontSize="20"
TextColor="#FF4081"
Text="WINDOW CLEANING"/>
<!--TextColor="#FF4081"-->
</ContentView>
</ListView.Header>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.ContextActions>
<MenuItem
Clicked="OnDelete"
CommandParameter="{Binding .}"
Text="Delete"
IsDestructive="True" />
</ViewCell.ContextActions>
<StackLayout
ClassId="{Binding Id}"
Orientation="Vertical">
<StackLayout.GestureRecognizers>
<TapGestureRecognizer
Tapped="Category_Tapped"
NumberOfTapsRequired="1" />
</StackLayout.GestureRecognizers>
<StackLayout
BackgroundColor="#e0e0e0"
Orientation="Horizontal"
VerticalOptions="FillAndExpand"
Padding="5"
Grid.Column="0">
<Image
Source="{Binding ImageSource}" />
<Label
Margin="10,0,0,0"
VerticalOptions="Center"
HorizontalOptions="StartAndExpand"
TextColor="#FF4081"
FontSize="17"
Text="{Binding Name}" />
<Label
Margin="0,0,10,0"
VerticalOptions="Center"
HorizontalOptions="End"
TextColor="DarkCyan"
Font="Bold, 20"
Text="{Binding ShortedAmount}" />
</StackLayout>
<StackLayout
Orientation="Horizontal"
IsVisible="{Binding IsVisible, Mode=OneWayToSource}">
<Label
HorizontalOptions="StartAndExpand"
Margin="5,10,0,20"
TextColor="DarkCyan"
Font="Bold, 16"
Text="{Binding Text}"/>
<Label
HorizontalTextAlignment="End"
HorizontalOptions="End"
Margin="0,10,10,20"
TextColor="DarkCyan"
Font="Bold, 16"
Text="{Binding Amount}"/>
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
代码片段背后的代码:
mainListView.ItemsSource = CategoryFactory.Categories;
private void Category_Tapped(object sender, EventArgs e)
{
string id = ((StackLayout)sender).ClassId;
id = id.Replace("CAT", "");
int i = Convert.ToInt32(id);
CategoryFactory.Categories[i].IsVisible = !CategoryFactory.Categories[i].IsVisible;
}
我想你应该已经明白了。 我想在运行时创建一个可扩展列表
您的模型/视图模型 class 必须实现INotifyPropertyChanged
才能通知 UI 属性更改
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.