【发布时间】:2018-02-07 01:36:29
【问题描述】:
我有一个 ListView 并通过 DataBinding 将它填充到我的 ViewModel 中的 Lists 属性。此外,我有一个 ListView 菜单,带有一个删除命令,也绑定到我的 ViewModel。 我现在的问题是,如果我初始化了 ListView,我可以删除其中的列表。如果我添加新列表,我可以删除所有列表。但是,如果我添加新项目,我无法删除它们,因为我从 DeleteCommand 获得的列表是旧的、已删除的列表。
因此,在删除列表后,它们似乎以某种方式仍然存在,如果当前列表的总数高于以前删除的列表的数量,我只能删除新列表。
我希望这是对我的问题的某种可以理解的解释。
绑定正在工作,并且我的 ViewModel 中的 Lists 属性具有正确的值,但 DeleteListCommand 中的“发送者”ItemList 是旧的 ItemList。
这是我的 ListView 的 XAML:
<ListView x:Name="listView" ItemsSource="{Binding Lists}" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell x:Name="viewCell">
<ViewCell.ContextActions>
<MenuItem Command="{Binding BindingContext.RenameListCommand, Source={x:Reference listView}}" CommandParameter="{Binding .}" Text="Rename" />
<MenuItem Command="{Binding BindingContext.DeleteListCommand, Source={x:Reference listView}}" CommandParameter="{Binding .}" IsDestructive="True" Text="Delete" />
</ViewCell.ContextActions>
<ContentView Margin="0,2,0,2"
HeightRequest="50"
BackgroundColor="{Binding Color}">
<ContentView.GestureRecognizers>
<TapGestureRecognizer BindingContext="{Binding Source={x:Reference listView}, Path=BindingContext}"
Command="{Binding ListTappedCommand}"
CommandParameter="{Binding Source={x:Reference viewCell}, Path=BindingContext}" />
</ContentView.GestureRecognizers>
<ContentView.Content>
<Label Text="{Binding Name}"
HorizontalTextAlignment="Center"
VerticalTextAlignment="Center"
TextColor="White"
IsEnabled="True"/>
</ContentView.Content>
</ContentView>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
这是我的 ViewModel:
...
public ObservableCollection<ItemList> lists = new ObservableCollection<ItemList>();
public ObservableCollection<ItemList> Lists
{
get { return lists; }
set
{
lists = value;
OnPropertyChanged("Lists");
}
}
public event PropertyChangedEventHandler PropertyChanged;
...
this.DeleteListCommand = new Command<ItemList>((sender) =>
{
OnDeleteList(sender);
});
...
public ICommand DeleteListCommand { get; set; }
private void OnDeleteList(ItemList itemList)
{
Lists.Remove(itemList);
}
...
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
【问题讨论】:
-
什么是“项目列表”?它也可以观察到吗?
-
它只是一个普通类,用作我的数据库的模型,用于带有字符串和布尔属性的 sqlite-net-pcl。我还将这个类用于 ObservableCollection,这样我就不必强制转换为不同的类型。我应该如何使它可观察?
-
所以,这不是一个列表吗?我知道您正在使用嵌套列表。对吗?
-
我想这会回答你的问题。不幸的是,我无法将其发布为答案,因为我无法将其归功于 forums.xamarin.com/discussion/86578/…
-
该错误已在 bugzilla 上报告,ID 为 42516:bugzilla.xamarin.com/show_bug.cgi?id=42516
标签: c# xaml listview data-binding xamarin.forms