Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In order to solve this, when the parent view creates the child view, have it inject a "remove" command into the view model at creation time as below.</p> <pre><code>//Parent var model = new ChildControlViewModel(); var view = new ChildControl(model); Children.Add(view); model.RemoveCommand = ()=&gt;Children.Remove(view); </code></pre> <p>Obviously this is massively simplified, also I'd make sure that you have a view control factory that ties up to a viewmodel, even having this in the code behind seems... wrong to me.</p> <p>Also, you can use the messenger and register for messages alternatively which IS an acceptable way to notify the view of the view model's action. For instance, I use this to register view->view interactions for a zoom slider in one of my projects. This approach COULD be used for removal.</p> <pre><code> public FlickrPhotoBrowserViewModel() { Photos = new ObservableCollection&lt;FlickrPhotoViewModel&gt;(); Messenger.Default.Register&lt;ZoomChangedMessage&gt;(this, message =&gt; ImageSize = message.Content); } </code></pre> <p>When I create a photo:</p> <pre><code> public async Task CreatePhotoContainerAsync(string photoID, IImageSearchService searchService, IStreamService streamService) { if (photoID != null) { var Photo = new FlickrPhotoViewModel(photoID); Photo.ShowDetailsCommand = new RelayCommand(() =&gt; { FlickrPhotoDataViewer.ShowPhoto(photoID, searchService, streamService); }); // REMOVE CODE!!! ^_^ Photo.RemovePhotoCommand= new RelayCommand(() =&gt; { Photos.Remove(Photo); FlickrPhotoDataViewer.CleanupPhotoElement(Photo); }); // REMOVE CODE!!! ^_^ //update async collection from bound UI thread DispatcherHelper.CheckBeginInvokeOnUI(() =&gt; Photos.Add(Photo)); var target = await searchService.GetPhotoThumbnailUriAsync(photoID); Photo.ImageStream = await streamService.GetStreamAsync(target); Debug.WriteLine("Image Download Complete " + target); } else { Debug.WriteLine("Image NULL " + photoID); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload