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
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.
You are correct.
Sorry for very late response.
- Ross
-Hector
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;
}
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
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...
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
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
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.