Operation not supported on read-only collection solution-[Windows Phone development skills Series 1]

Source: Internet
Author: User

Now we will start to create a series of records on the solutions to the problems we encountered when learning and developing the Windows Phone project, as well as some development tips, hoping to play a gentle and informative role. In Windows Phone development, it is often used to deal with ListBox. ListBox is also a very convenient control. Especially after data binding, you can learn a lot about ListBox, there are also a lot of ListBox applications in the garden.ArticleBefore using ListBox, you wanted to use the dynamic backgroundCodeBut encountered the error "operation not supperted on read-only Collection.

 

Finally, the problem is solved, but it is found that there are many important points in the solution process, not only in Windows Phone development, but also in wpf development and Silverlight development. Here is a simple example to illustrate how to solve the problem.

Create a project [listboxdemo]. Place a ListBox In The XAML file and bind the data. We use the background code to set the itemsource of ListBox.

<Stackpanel X: Name = "contentpanel" grid. row = "1" margin = "12, 0, 12, 0"> <ListBox X: Name = "listbox1"> <ListBox. itemtemplate> <datatemplate> <textblock text = "{binding}" style = "{staticresource phonetextnormalstyle}"/> </datatemplate> </ListBox. itemtemplate> </ListBox> <button X: Name = "deletebtn" content = "delete data" Click = "deletebtn_click"/> <button X: name = "addbtn" content = "add data" Click = "addbtn_click"/> <button X: name = "clearbtn" content = "Clear ListBox" Click = "clearbtn_click"/> </stackpanel>
 
Private void initializelistbox () {list <string> source = new list <string> {"Item1", "item2", "item3", "item4", "item5 ", "item6"}; this. listbox1.itemssource = source ;}

After the ListBox is initialized, we want to use three buttons to dynamically change the data. If we want to delete the first listboxitem, the following code is used to achieve this result.

 
// Delete data private void deletebtn_click (Object sender, routedeventargs e) {If (this. listbox1.items. count> 0) {This. listbox1.items. removeat (1) ;}}// add private void addbtn_click (Object sender, routedeventargs e) {This. listbox1.items. add ("item7");} // clear the list private void clearbtn_click (Object sender, routedeventargs e) {This. listbox1.items. clear ();}

 

The problem we encountered here is that if you bind a data source through the background, these items are internally set to readonly. If you want to delete or add data, only when these item sets implement the inotifycollectionchanged interface can we modify them dynamically.

So the solution is very simple. You only need to let the item set implement the inotifycollectionchanged interface and implement it. In fact, another simpler method is to directly use the observablecollection class to replace the list, because the observablecollection has implemented the inotifycollectionchanged interface. For more information about the observablecollection, click here to view the description. At the same time, the itemsource of ListBox is converted to observablecollection, so that we can modify it.

Note the [clear ListBox] function. The [clear] function is used before. In this way, an error is returned, the correct method should be to set itemsource of ListBox to null to solve this problem.

 

="

 

The observablecollection class deserves more research. For example, [list vs observablecollection vs inotifypropertychanged in Silverlight] And the msdn blog both provide detailed information on its usage.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.