如何在windowsphone中将字符串列表转换为可观察集合

本文关键字:转换 观察 集合 列表 字符串 windowsphone | 更新日期: 2023-09-27 17:51:19

嗨,我有一个类似的列表字符串

 List<string> numbers=new List<string>();

现在我想在可观察的集合上转换它,我已经像一样成功地转换了它

ObservableCollection<string> myCollection = new ObservableCollection<string>(numbers);

但是当我从这样的列表框中删除项目时

myCollection.Remove(listBox1.SelectedItem.ToString());
        listBox1.ItemsSource = myCollection;

上面的代码删除了列表框中的所有项目,但我想删除列表框中特定的项目。

如何在windowsphone中将字符串列表转换为可观察集合

试试这个

初始化集合和lsitbox

 List<string> numbers=new List<string>();
 //numbers.Add("test");  //populate list
  ObservableCollection<string> myCollection = new ObservableCollection<string>(numbers);
  listBox1.ItemsSource = myCollection;

现在使用下面的代码从列表中删除所选项目

var selectedItem =listbox1.SelectedItem as string;
if(myCollection.Contains(selectedItem)
   {
      myCollection.Remove(selectedItem);
   }

您可以直接将List<string>绑定到listBox1.ItemsSource,而不是绑定ObservableCollection。请参阅此示例绑定List<string>

windows phone数据绑定listpicker到字符串列表

要从列表中删除项目,请尝试此

listBox1.Items.Remove(listBox1.SelectedItem);

答案:
数字。RemoveAt(listBox1.SelectedIndex(;

        listBox1.ItemsSource = null;
        listBox1.ItemsSource = numbers;