Regarding treeview using MVVM pattern
-
Hi.. I am using hierarchical data template in tree view in my wpf application using mvvm pattern. Binding of data using observable collection done successfully through viewmodel.. But my problem is in getting selected item.. Can anybody tell me how to get selected item in treeview in mvvm pattern.....?
-
Hi.. I am using hierarchical data template in tree view in my wpf application using mvvm pattern. Binding of data using observable collection done successfully through viewmodel.. But my problem is in getting selected item.. Can anybody tell me how to get selected item in treeview in mvvm pattern.....?
Try reading Josh's article on MVVM with the Treeview[^]. Download the code and see how Josh does it.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
Hi.. I am using hierarchical data template in tree view in my wpf application using mvvm pattern. Binding of data using observable collection done successfully through viewmodel.. But my problem is in getting selected item.. Can anybody tell me how to get selected item in treeview in mvvm pattern.....?
I only tested this in Silverlight, should be the same in WPF though. You can in fact bind to SelectedItem on the TreeView
<controls:TreeView ItemsSource="{Binding KeywordSource}" SelectedItem="{Binding SelectedKeyword, Mode=TwoWay}">
Blend throws an error, noting (correctly) that SelectedItem has not have an accessible setter, which is also the reason you will not get the SelectedItem in Intellisense. If I understand correctly however, you just need the getter, which is working for me. -
I only tested this in Silverlight, should be the same in WPF though. You can in fact bind to SelectedItem on the TreeView
<controls:TreeView ItemsSource="{Binding KeywordSource}" SelectedItem="{Binding SelectedKeyword, Mode=TwoWay}">
Blend throws an error, noting (correctly) that SelectedItem has not have an accessible setter, which is also the reason you will not get the SelectedItem in Intellisense. If I understand correctly however, you just need the getter, which is working for me.Setting the Mode on the Binding of the SelectedItem to OneWayToSourceeliminates the error message
<controls:TreeView ItemsSource="{Binding KeywordSource}" SelectedItem="{Binding SelectedKeyword, Mode=OneWayToSource}">
I lied, throws a compile time error...