0

I want to Bind to ViewModel the selected Node of the Treeview , notice that every node is a NavPoint (from the EPUB's Table Of Content) , How create a dependency property "SelectedNode" on Control that Binds the Selected Node in the correct Type . I used

<TextBlock Text="{Binding SelectedItem ,ElementName=myTreeview}"/>

but i want to bind it to my MainWindowViewModel.cs class to manipulate it and make some changes over it !

Rohit Vats
  • 79,502
  • 12
  • 161
  • 185

1 Answers1

0

If you handle the TreeView.SelectedItemChanged Event, then you could update your SelectedNode property:

private void SelectionChanged(object sender, RoutedPropertyChangedEventArgs<Object> e)
{
    YourSelectedNodeProperty = (TreeViewItem)e.NewValue;
}
Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • I think the question is using MVVM pattern. this works but I hope someone can shed light on how to do this using MVVM – Rob Jul 12 '18 at 09:51
  • @Rob, to use this method with MVVM, simply wrap this event in an Attached Property. I have documented an example of this in my answer to the [What's the best way to pass event to ViewModel?](https://stackoverflow.com/questions/21285577/whats-the-best-way-to-pass-event-to-viewmodel/21285863#21285863) question, here on Stack Overflow. – Sheridan Aug 13 '18 at 16:15