WPF Binding Treeview element to a UserControl?

Since you're using MVVM, you can alternativly put that kind of logic in the view model. You can then bind the IsSelected property of tree node to your viewmodel, then when IsSelected get set to true by wpf (when the use selects the item) you can do whatever you want.

Up vote 0 down vote favorite share g+ share fb share tw.

I have a tree view that is using the Model View architecture, each TreeViewItem has a windows Form attribute, when I click on a node I want the application to display the form associated with that node to the right hand side of the tree. How can you achieve this using binding I have tried the following but the user control Associated with ApplicationForms doesn't get displayed. C# wpf xaml treeview link|improve this question edited May 5 '11 at 14:16H.B.43.4k61646 asked May 5 '11 at 13:42Jon61.

– H.B. May 5 '11 at 14:20 (Binding errors are found in the Output window of Visual Studio) – H.B. May 5 '11 at 15:51 I am not 100% sure but I think the fact that I am trying to display a WindowsForm might be the problem as the should be hosted in a WindowsFormsHost. However, it seems windows forms controls don't support binding so I will be forced to have the ApplicationsViewModel make a call to top level application directly. – Jon May 5 '11 at 16:45.

Since you're using MVVM, you can alternativly put that kind of logic in the view model. You can then bind the IsSelected property of tree node to your viewmodel, then when IsSelected get set to true by wpf (when the use selects the item) you can do whatever you want. Its a very useful pattern to use view models this way.

Your viewmodels can have references to all kinds of stuff and affect them based on selection or expansion. You can also go the other way around and have code affect the viewmodels and let the databinding update the actual controls Here is a pretty good article on MVVM and treeview You should also check out the HierarchicalDataTemplate if you're working with treeviews -edit- After reading the question properly, I see that you're already doing the right thing, that is binding your master control to the SelectedItem of the Treeview. I do belive the SelectedItem property points to the TreeViewItem though, not the actual VM.

Perhaps thats the problem?

I think you might have missed the point there. – H.B. May 5 '11 at 14:19 Maybe:) I got the impression OP wants to show the content beside the treeview, in a Master-Detail kind of scenario. – aL3891 May 5 '11 at 14:25 Yes, but the master is done I think, this is only about the detail.

– H.B. May 5 '11 at 14:33 Yes sorry, I possibly didn't make it clear the Treeview side of things is working correctly. Its the Detail side that I can't get to display. I am new to WPF and not sure if I should be trying to bind to a control?

– Jon May 5 '11 at 14:41 hm, you're probably right, I didn't scroll his code enough to the right but now I see he's already correctly bound it to the treeview.. its probably a binding path error like you say. – aL3891 May 5 '11 at 14:41.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions