silverlight Treeview。 Items.Add()抛出异常Message =“Value不在预期范围内

时间:2014-11-13 10:33:21

标签: silverlight treeview

我正在使用silverlight treeview控件。在运行时,我将项添加到树中。在一种情况下,我正在清除特定节点中的项目,然后将新项目添加到该节点。将项添加到节点时,我收到异常消息=“值不在预期范围内。”

我首先清除节点中的项目然后添加,但仍然获得异常。

下面是堆栈跟踪-----

在MS.Internal.XcpImports.CheckHResult(UInt32 hr)    在MS.Internal.XcpImports.Collection_AddValue [T](PresentationFrameworkCollection 1 collection, CValue value) at MS.Internal.XcpImports.Collection_AddDependencyObject[T](PresentationFrameworkCollection 1集合,DependencyObject值)    在System.Windows.PresentationFrameworkCollection 1.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.PresentationFrameworkCollection 1.添加(T值)    在SilverlightUI.SelectFolder.AddDataToNode()    在SilverlightUI.SelectFolder。<> c__DisplayClass35.b__34() 请帮助解决此问题。

感谢。

1 个答案:

答案 0 :(得分:0)

您尝试将项目直接添加到TreeView.Items集合时看起来很可疑。我会修改ItemsSource。当ItemsSource更改时,TreeView.Items集合的内容由树视图控件保持同步。

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

并在您的viewmodel中:

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

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