I have several controls in my WPF window. I have divided the window into 4 sections. If I click on the section in the upper left, I want to copy the contents of this section in another window popup.
What I want to copy is a grid that can contain a lot of different controls, ex: togglebutton, button, panel… derived from ItemsControl, Control… and with DependencyProperty, ObservableCollection etc..
I tried :
- XamlWriter.Save(data);
- XamlServices.Save(data);
but I always have these errors :
- Cannot serialize a generic type ‘System.Collections.ObjectModel.ObservableCollection`1 or
- A BadImageFormatException has been thrown while parsing the signature. This is likely due to lack of a generic context. Ensure
genericTypeArguments and genericMethodArguments are provided and
contain enough context.
You should not clone XAML itself, this is brutal and wrong way.
Take a look at the possible MVVM solution below. Let’s say single DataGird represents a single business Item, so you need following:
ObservableCollection<Item> Items { get; set; })ICommand CopyItemand in command handler actually copyiing only instance of the Item business entity and adding it to theItemslist, WPF reflects this changes via bindings and UI will be updated by a new ListViewItem with a DataGrid representing a details of just copied ItemUseful links: