WPF Application .Net 6.0 Visual Studio
CategoryList_Copy.Items.Clear()
Dim newNode As ItemCollection = CType(CategoryList.Items.Clone(), ItemCollection)
CategoryList_Copy.Items.Add(newNode)
How can I make this work? I am trying to copy one item from a TreeView to another TreeView.
I am using a NuGet package called AnyClone for this, but I think I'm doing something wrong.
I get this error:
System.MissingMethodException: 'Constructor on type 'System.Windows.Controls.ItemCollection' not found.'
What am I doing wrong?
CodePudding user response:
First of all, the error is very easily explained: ItemCollection doesn't have a public constructor with 0 parameters. AnyClone obviously has to instanciate the item it wants to clone, which ItemCollection does not support, at least not without supplying parameters.
This shouldn't matter though, you're just using the wrong approach to copying your data.
ItemCollection is a View, that - from the supplied data binding - is used as a layer between your data and your TreeView, so you can apply sorting/filtering/grouping without changing the data. If you want to copy data from one TreeView to another, don't copy the data in the ItemCollection, copy the data in the ItemsSource.
Also, you're trying to clone all of the items, instead of just the one you want to copy. Even if you get around the error above: you try to copy one TreeView's complete view and then add it as an item to the other TreeView, which isn't what you're trying to achieve.
A much cleaner approach would be the following:
TreeView
should have an ItemsSource
: Bind this ItemsSource to the data you want your TreeView to display. Make sure CategoryList_Copy
has a separate collection bound to its ItemsSource. When copying data, just copy the value from one CategoryList's ItemsSource over to the other. There you should be able to easily use AnyClone's cloning method.