I have a library of custom classes with the classes MyTabItem, MyTabControl, MyGrid
. When you drag MyTabItem
(has a value of ViewModel) on the MyGrid
, the MyGrid_Drop
event is triggered. At this point, MyTabControl
is created and I need to create a collection (ObservableCollection <ViewModelBase> Pages
) in the application namespace and specify it in the MyTabControl.ItemsSource
. After add MyTabItem
to this collection.
Code in the application:
public ObservableCollection<ViewModelBase> Pages { get; set; } = new ObservableCollection<ViewModelBase>();
Code in the library:
private void MyGrid_Drop(object sender, DragEventArgs e)
{
if (e.Data.GetData(typeof(MyTabItem)) is MyTabItem tabItemSource)
{
if (InstanceTabControl.ItemsSource == null)
{
// Here I need to create a collection of ViewModels with the type as the TabItemSource.Content.
}
if (InstanceTabControl.ItemsSource is IList list)
{
var source = tabItemSource.Content;
list.Remove(source);
list.Add(source);
InstanceTabControl.SelectedIndex = list.Count - 1;
}
else
{
InstanceTabControl.Items.Remove(tabItemSource);
InstanceTabControl.Items.Add(tabItemSource);
InstanceTabControl.SelectedIndex = InstanceTabControl.Items.Count - 1;
}
}
}
By reference sample project PeopleDemo.
Func <T, TResult>
delegate to create a collection in the application, but can I do without it?Pages
collection is in ShellViewModel
. It seems that I will need this collection into a collection of subcollections of view models and then for each created MyTabControl
in the library to create my own subcollection. But how then to bind such a subcollection in xaml (<adc:MyTabControl ItemsSource="{Binding Pages}">
)?Please direct me how to solve this problem?
I solved this problem. Inside the library, I use the ObservableCollection<object> Pages
collection. Each instance of MyTabControl has its own collection.
I can pass a type through a generic to the class that controls the creation of collections.