This is a migrated thread and some comments may be shown as answers.

Operation not supported on read-only collection

10 Answers 245 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Ross
Top achievements
Rank 1
Ross asked on 29 Oct 2008, 02:23 AM
Hello!

I am having problems adding and editing RadTreeViewItem. I am using HierarchicalDataSource : ObservableCollection when binding the RadTreeView. When I try to add new or edit item via code behind (i.g. on selected item) it throws an InvalidOperationException (Operation not supported on read-only collection.)

Please help!

Thanks in advance!

10 Answers, 1 is accepted

Sort by
0
Accepted
Valentin.Stoychev
Telerik team
answered on 30 Oct 2008, 07:59 AM
Hello Ross,

when you are using - the TreeView (or in fact any other ItemsControl) with an ItemsSource, you can not add/remove elements using the Items collection. You should modify your underlying collection instead.

Sincerely yours,
Valentin.Stoychev
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Ross
Top achievements
Rank 1
answered on 14 Nov 2008, 02:25 AM
Thanks for the reply and direction Valentin!

You are correct.

Sorry for very late response.
- Ross
0
Hector
Top achievements
Rank 1
answered on 22 May 2009, 04:06 PM
Hello, I am experiencing the same problem.  I have a combobox bound to a WCF service that populates a list of reports. I don't want to display an item immidiately, I want the selected index to say "Please select a report from list".  So I created an item and added it to the ComboBox.Items List after I bound the control.  Is there a way to display default text on a combobox?  Thanks.

-Hector
0
Hector
Top achievements
Rank 1
answered on 22 May 2009, 04:12 PM
Valentin, I figured it out by re-reading your post.  I modified the underlying source.  Here is my code:

 

void proxy_GetReportListCompleted(object sender, sl_swr.PortalService.GetReportListCompletedEventArgs e)

 

{

 

    // Add a default text to list

 

    sl_swr.PortalService.

ReportList selectList = new sl_swr.PortalService.ReportList();

 

    selectList.ReportName =

"Select a report from this list";

 

    e.Result.Insert(0,selectList);

    cmbSelectReport.ItemsSource = e.Result;

}

0
al
Top achievements
Rank 1
answered on 29 Jun 2009, 01:45 AM
can you show us how to do it?! because i think i have already done that and still i am facing the same problem... my concern is also on the treeview.
0
Hector
Top achievements
Rank 1
answered on 29 Jun 2009, 10:54 AM
Al,
can you post you code where you are setting the source for the treeview?  You need to add the items to the source it self.  In my example, my suorce is e.Result, so this is how I insert the item I want:
MyDataObject newObject= new MyDataObject();
newObject.ReportName = "Inserted item one.";

e.Result.Insert(0,newObject);

-Hector

0
al
Top achievements
Rank 1
answered on 30 Jun 2009, 01:53 AM
HI Hector!
I have already fixed the problem. Therese a textblock inserted inside the treeview, i removed that one and everthing works fine. Thanks for you help...
0
Mark Huck
Top achievements
Rank 1
answered on 28 Sep 2009, 06:57 PM
Valentin,

What changes would you make to the treeview sample code to accomplish this when using ItemsSource?  Here's the demo code:

private

 

void AddItem()

 

{

 

RadTreeViewItem treeViewItem = RadTreeView1.SelectedContainer;

 

 

if (treeViewItem == null)

 

{

 

this.RadTreeView1.Items.Add(new RadTreeViewItem()

 

{

Header =

"New Node",

 

});

 

return;

 

}

treeViewItem.Items.Add(

new RadTreeViewItem()

 

{

Header =

"New Node",

 

});

treeViewItem.IsExpanded =

true;

 

}

Thanks!
Mark

0
Mark Huck
Top achievements
Rank 1
answered on 29 Sep 2009, 03:14 AM
Valentin,

Tried this approach (using a different sample from Telerik), but the item was not added ....


public

 

MainPage()

 

{

InitializeComponent();

 

this.RadTreeView1.AddHandler(RadMenuItem.ClickEvent, new RoutedEventHandler(OnMenuItemClicked));

 

 

Mouse.AddMouseDownHandler(this.RadTreeView1, OnRightMouseButtonUp);

 

 

this.source = new TreeDataSource();

 

RadTreeView1.ItemsSource = source;

}


private

 

void OnMenuItemClicked(object sender, RoutedEventArgs args)

 

{

Telerik.Windows.

RadRoutedEventArgs e = args as Telerik.Windows.RadRoutedEventArgs;

 

 

RadMenuItem item = e.OriginalSource as RadMenuItem;

 

 

if ((item == null))

 

{

 

return;

 

}

 

switch (item.Header.ToString())

 

{

 

case "Add New Item":

 

 

this.AddItem(e);

 

 

break;

 

}

}



private

 

void AddItem(Telerik.Windows.RadRoutedEventArgs e)

 

{

 

// add item to list

 

 

this.source.Add(new Organization("Newly added item"));

 

RadTreeView1.ItemsSource = source;


Thanks for helping me with this!
Mark

0
Valentin.Stoychev
Telerik team
answered on 29 Sep 2009, 09:13 AM
Hello Mark Huck,

The information provided is not enough for us to identify the problem. Can you please open a new support ticket and send us a sample project which we can investigate.

Thanks in advance!

All the best,
Valentin.Stoychev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
TreeView
Asked by
Ross
Top achievements
Rank 1
Answers by
Valentin.Stoychev
Telerik team
Ross
Top achievements
Rank 1
Hector
Top achievements
Rank 1
al
Top achievements
Rank 1
Mark Huck
Top achievements
Rank 1
Share this question
or