Operation not supported on read-only collection 的解决方法 - [Windows Phone开发技巧系列1]
发布日期:2025-04-29 07:41:49 浏览次数:17 分类:精选文章

本文共 1832 字,大约阅读时间需要 6 分钟。

Windows Phone???ListBox????????????????

???Windows Phone????ListBox??????????UI??????????????????????Operation not supported on read-only collection???????????????????????????????????????????????????????

????

??????ListBox?????????????????????ListBox?ItemSource???????????List<string>?????????????????????????ListBox?ItemSource?????????????????????????

????

??????????????ListBox?ItemSource?????ObservableCollection????????ListBox?????????????????? CRUD???????????????????????????????

1. ??ObservableCollection

????????????????????????????????????

using System.Collections.Generic;using System.Linq;public class MyViewModel{    private readonly ObservableCollection
_items = new ObservableCollection
{ "item1", "item2", "item3", "item4", "item5", "item6" }; public ObservableCollection
Items { get => _items; private set => _items = value; }}

2. ??ListBox?ItemSource

?XAML???ListBox?ItemSource???

3. ??????

??ObservableCollection?????????ListBox????????????????????????

private void deleteBtn_Click(object sender, RoutedEventArgs e){    if (ListBox1.Items.Count > 0)    {        ListBox1.Items.RemoveAt(1);    }}private void addBtn_Click(object sender, RoutedEventArgs e){    ListBox1.Items.Add("item7");}private void clearBtn_Click(object sender, RoutedEventArgs e){    ListBox1.Items.Clear();}

4. ??????

??ObservableCollection???INotifyCollectionChanged???ListBox??????????????????????????????????????CollectionChanged??????

5. ????

  • ??DataContext?????ViewModel??DataContext?????????????????
  • ???????ObservableCollection?????????UI???????UI???????????
  • ???????????????????????????????????

??

???ListBox?ItemSource???ObservableCollection?????????????????????????????????????????????UI???????????????Windows Phone?????????????????????????????????????????

上一篇:OperationResult
下一篇:Opera Mobile Classic Emulator

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2026年06月14日 18时48分11秒