1
votes

I am using silverlight treeview control. At runtime I am adding items into the tree. In one case , I am clearing the items from particular node and then add new items to that node. While adding the items to the node I am getting exception Message = "Value does not fall within the expected range."

I am first clearing the items from the node then adding, but still getting the exception.

Below is the stack trace-----

at MS.Internal.XcpImports.CheckHResult(UInt32 hr) at MS.Internal.XcpImports.Collection_AddValue[T](PresentationFrameworkCollection1 collection, CValue value) at MS.Internal.XcpImports.Collection_AddDependencyObject[T](PresentationFrameworkCollection1 collection, DependencyObject value) at System.Windows.PresentationFrameworkCollection1.AddDependencyObject(DependencyObject value) at System.Windows.Controls.UIElementCollection.AddInternal(UIElement value) at System.Windows.Controls.ItemsControl.AddVisualChild(Int32 containerIndex, DependencyObject container, Boolean needPrepareContainer) at System.Windows.Controls.ItemsControl.AddContainerForPosition(GeneratorPosition position) at System.Windows.Controls.ItemsControl.OnItemsChangedHandler(Object sender, ItemsChangedEventArgs args) at System.Windows.Controls.ItemContainerGenerator.OnItemAdded(Object item, Int32 index, Boolean suppressEvent) at System.Windows.Controls.ItemContainerGenerator.System.Windows.Controls.ICollectionChangedListener.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args) at System.Windows.Controls.WeakCollectionChangedListener.SourceCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e) at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e) at System.Windows.Controls.ItemCollection.NotifyCollectionChanged(NotifyCollectionChangedEventArgs e) at System.Windows.Controls.ItemCollection.AddImpl(Object value) at System.Windows.Controls.ItemCollection.AddInternal(Object value) at System.Windows.PresentationFrameworkCollection1.Add(T value) at SilverlightUI.SelectFolder.AddDataToNode() at SilverlightUI.SelectFolder.<>c__DisplayClass35.b__34() Please help to resolve this issue.

Thanks.

1

1 Answers

0
votes

It looks quite suspicious that you try to add items directly to the TreeView.Items collection. I would modify the ItemsSource. The content of the TreeView.Items collection is kept in sync by the treeview control when the ItemsSource changes.

<TreeView ItemsSource="{Binding MyItems}"/>

and in your viewmodel:

public ObservableCollection<Foo> MyItems { get; set; }

private void ChangeStuff()
{
    MyItems.Add(new Foo());
}