There are 2 view models, the main and view models that correspond to the created collection

The main view model is MainVM . It has a command that imitates the logic of getting, creating and adding view models for this collection, as well as adding the resulting collection to the data model class for later work with it.

public class MainVM : Notify { public ObservableCollection<CollectionVM> CollectionsVM { get; } public ICommand AddSampleCollectionCommand { get; } private Collections _collections; public MainVM() { _collections = new Collections(); CollectionsVM = new ObservableCollection<CollectionVM>(); AddSampleCollection = new RelayCommand(AddSampleCollection); } private void AddSampleCollection() { var collection = new List<SimpleType>(); var vmForCollection = new CollectionVM() { Title = "Test title" }; _collections.Add(collection); CollectinsVM.Add(_collectinsVM); } } 

Viewmodel Collection - CollectionVM

 public class CollectionVM : Notify { public string Title { get; } //Проблема в реализации этой команды. public ICommand DeleteCommand { get; } } 

Class storage for collections - Collections . I did not copy it completely, but described its structure briefly.

 public class Collections { private List<List<SimpleType>> _collestions; public void Add(List<SimpleType> item) => _collestions.Add(item); public void Delete(List<SimpleType> item) => _collestions.Remove(item); } 

The xaml markup itself looks something like this. I took ItemsControl to display the viewmodels (I just need to display items, select them, etc., they don’t need them, so I took it)

 <ItemsControl x:Name="CollectionsList" HorizontalContentAlignment="Stretch" ItemsSource="{Binding CollectionsVM}" ItemTemplate="{StaticResource CollectionDataTemplate}" BorderThickness="0"> </ItemsControl> 

Well, xaml DateTemplate

 <DataTemplate x:Key="CollectionDataTemplate"> <StackPanel> <TextBlock Text="{Binding Title}"/> <Button x:Name="DeleteButton" Command="{Binding DeleteCommand}"/> </StackPanel> </DataTemplate> 

The problem is the following: I need to delete the corresponding view model and collection instance from the Collections class by clicking the DeleteButton button. Since there is no connection between the view model and the required class instance, I do not understand how to implement the deletion of both. I thought I would add a property to the collection model with reference to the required instance of the collection, but I was told that it was better not to do so. Tell me how to implement it correctly and maybe something completely remake in the code above. thank

  • You have essentially 3 options. 1. Send the link to the correct VM to the one where you have the necessary command / action ( ... new CollectionVM(this); ). I do not know why they are telling you that this is not correct, a completely standard solution. 2. In CollectionVM implement an event that will alert you that the object should be deleted. When you initialize a new object of this VM, subscribe and perform the necessary actions. 3. Make a delete command on the layer above (pull out from CollectionVM ) and bind it via RelativeSource ( FindAncestor ) in XAML. - EvgeniyZ
  • Supporting references: Option 2 is one of my old answers . In it, I caused a delete event on a timer. Option 3 - for example, this answer . - EvgeniyZ
  • In general, I will make a property with a link, thanks - Alexander

0