Binding SelectedItem in a HierarchicalDataTemplate-applied WPF TreeView
问题 I have a data-bound TreeView and I want to bind SelectedItem . This attached behavior works perfectly without HierarchicalDataTemplate but with it the attached behavior only works one way (UI to data) not the other because now e.NewValue is MyViewModel not TreeViewItem . This is a code snippet from the attached behavior: private static void OnSelectedItemChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) { var item = e.NewValue as TreeViewItem; if (item != null) { item