c# – How to find its control by ViewModel?

Question:

Vm comes to SelectedItem from which control gets data. But how can I get the control itself, and not its Vm ?

 private void AssociatedObject_DragOver(object sender, DragEventArgs e)
        {
            var selectedItem = this.AssociatedObject.SelectedItem;
            var module = selectedItem as ModuleVm;
            if (module != null)
            {
                //сделать IsSelected = false;
            }
        }

Answer:

The easiest way is to get the TreeViewItem from the VM of an individual item like this:

(TreeViewItem)treeView.ItemContainerGenerator.ContainerFromItem(vm)

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

Scroll to Top