简体   繁体   English

在Windwos Phone 8.1 Silverlight中,如何从列表框中删除选定的项目

[英]How to remove selected item from ListBox in windwos phone 8.1 Silverlight

I want to delete selected item from listbox in widnows phone I have tryied 我想从尝试过的Widnows手机的列表框中删除所选项目

myclass is : myclass是:

    public class mydata
    {
        public int text;

        public int Text
        {
            get;
            set;
        }
    }

And my code which I bind to List is 我绑定到列表的代码是

    for (int i = 0; i < 15; i++)
    {
        lstd.Add(new mydata() { Text = i });
    }
    txt.ItemsSource = lstd;

I have write this code on selectionchanged event of listbox 我在listbox的selectionchanged事件上写了这段代码

txt.Items.Remove(txt.SelectedItem);

but it give an error "Operation not supported on read-only collection." 但是会出现错误“只读集合不支持操作”。

I have also try this one 我也尝试过这个

 var item = lb.SelectedItem as mydata;
            lstd.Remove(item);
            txt.ItemsSource = lstd;
            txt.UpdateLayout();

Item is removed from lstd but can not update layout of listbox; 项目已从lstd中删除,但无法更新列表框的布局;

I do not want to write this line after removing item from Listbox 从列表框中删除项目后,我不想写此行

txt.ItemsSource = null;

Please help me to sort out this. 请帮我解决这个问题。

Thanx in advance 提前感谢

如果listed对象的类是List<mydata> ,则将其更改为ObservableCollection<mydata> ,因此将通知列表框lstd更改。

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

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