In SelectedItem comes Vm from which the control gets data. But how can I get myself control, not his Vm ?

  private void AssociatedObject_DragOver(object sender, DragEventArgs e) { var selectedItem = this.AssociatedObject.SelectedItem; var module = selectedItem as ModuleVm; if (module != null) { //сделать IsSelected = false; } } 
  • By the way, instead of double if you can immediately write this.AssociatedObject.SelectedItem as ModuleVm . Unless, of course, you still have no additional logic there after the second if . - andreycha
  • @andreycha; Yes, indeed. - Lightness
  • No, you shouldn't need this. Drag-and-drop should be handled by the UI, not the VM. - VladD
  • @VladD, It just so happens that we have a Drag-n-Drop VM . And I really need to somehow get out of this situation. Maybe there is some way? - Lightness
  • @Lightness: Enumerate all controls, look at their DataContext, and build a VM mapping in a control? As a rough hack come down. And what type of your controls? Are they casually not elements of a listview or something like that? - VladD

1 answer 1

The easiest way is to get a TreeViewItem on the VM of a single item like this:

 (TreeViewItem)treeView.ItemContainerGenerator.ContainerFromItem(vm) 

Starting from it, you can either search in the depth of the ItemTemplate , or work with the container directly.