简体   繁体   English

从ListPicker移除选定的项目

[英]Removing the selected item from a ListPicker

I've got a Windows Phone app with a ListPicker bound to an ObservableCollection and a selected item: 我有一个Windows Phone应用,该应用的ListPicker绑定到ObservableCollection和一个选定的项目:

<toolkit:ListPicker Header="Endgame condition" ItemsSource="{Binding Conditions}" 
                    SelectedItem="{Binding SelectedCondition, Mode=TwoWay}" />

And in the ViewModel: 并在ViewModel中:

public ObservableCollection<string> Conditions
{
    get { return conditions; }
}

public string SelectedCondition
{
    get { return selectedCondition; }
    set
    {
        if (selectedCondition != value)
        {
            selectedCondition = value;
            OnPropertyChanged("SelectedCondition");
        } 
    }
}

When I try to remove the selected item from the list in a button handler, I get an InvalidOperationException (SelectedItem must always be set to a valid value): 当我尝试从按钮处理程序中的列表中删除选定的项目时,我收到一个InvalidOperationException(SelectedItem必须始终设置为有效值):

public void ConfirmCondition()
{
    var selected = selectedCondition;
    SelectedCondition = null;

    // throws an exception: SelectedItem must always be set to a valid value
    conditions.Remove(selected);
}

I was hoping that setting the SelectedCondition to null (which is actually the first item in the list), would fix it, but it doesn't. 我希望将SelectedCondition设置为null(实际上是列表中的第一项)可以解决该问题,但事实并非如此。 It appears that the NotifyPropertyChanged on the SelectedCondition (which is bound to the ListPicker's SelectedItem with a TwoWay binding mode) doesn't update the SelectedItem before the Remove triggers a CollectionChanged notification for the ItemsSource. 看来,在Remove触发ItemsItemSource的CollectionChanged通知之前,SelectedCondition上的NotifyPropertyChanged(已通过TwoWay绑定模式绑定到ListPicker的SelectedItem)不会更新SelectedItem。

Any ideas how I can remove the item without getting the exception? 有什么想法可以删除该项目而又不会导致异常吗?

While I can't be 100% certain based on the code you've shown it's probably because the "selected" item isn't actually a member of the "conditions" collection. 根据您显示的代码,我不能百分百确定它,这可能是因为“已选择”项实际上不是“条件”集合的成员。

Because you're using a separate object to track which of the collection is selected you'll need to find the equivalent item in the collection and delete that instead. 因为您使用的是单独的对象来跟踪选择了哪个集合,所以需要在集合中找到等效项并将其删除。 (You can't remove an item from a collection which isn't in the collection in the first place. Even if they are represented in the same way on screen.) (您不能从一开始就不在集合中的项目中删除项目。即使它们在屏幕上的显示方式相同。)

Try something like this. 尝试这样的事情。

foreach(var item in conditions)
{
    if(item.Equals(selectedCondition))
    {
        conditions.Remove(item);
    }
}

(Make sure Equals is suitably capable of correctly determining the correct instance in the collection.) (确保Equals能够正确确定集合中的正确实例。)

public ObservableCollection<string> Conditions
{
    get { return conditions; }
    set { return contitions;
    OnPropertyChanged("SelectedCondition");
    }
}

public void ConfirmCondition()
{
    if(SelectedCondition != null){
    conditions.Remove(SelectedCondition );
    }
}

work? 工作? @Matt Lacey If you add item like you remove item you don't need to parse ObservableCollection. @Matt Lacey如果您像删除项目一样添加项目,则无需解析ObservableCollection。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM