Click here to Skip to main content
15,071,899 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
below the question i posted still i did not got the answer also shared in the codeproject
my problem is i am having two controls tree view and list view for both i am using different classes MVVVM architect eg:- for list view separate view model for tree view separate view model,now i am coming to the point when i click the tree view items i need to update the list view from the treecontrol view model i am getting the data need to update in the listview

What I have tried:

c# - How to Bind the listview data from another class in wpf - Stack Overflow[^]
Updated 15-Jan-21 22:04pm

1 solution

In order to do that, you need access to both instances - and since one control shouldn't know about the other, that means going via the item that contains them both.
Have a look here: Transferring information between two forms, Part 3: Child to Child[^] - it's based on forms rather than controls but it's exactly the same process and even code with controls (and with WPF instead of Winforms).
Fazil13 16-Jan-21 4:08am
I am using wpf please take a look on my link i have posted a code of mine.
Hello, I have looked (for a short time) at your question and at stack overflow post. Here is my suggestion:
1. (optional)Place ViewModelBase and RelayCommand in separate files
2. Create two viewmodels - one for single NodeDetails, the data class you are using, and one for all node details, i.e. the collection you are using.
3. In view model for single Node detail, all properties that matter should call NotifyPropertyChanged in their set accessor. Yes, property getter setter for Name, for Size, for DateModified etc. ImageSource is tricky, it might be handled few ways other than this.
4. In view model for all node details, your ObservableCollection should hold not NodeDetail objects, but NodeDetailsViewModel objects. Even better, try to use CollectionViewSource instead of raw ObservableCollection. ObservableCollection is useful for tracking adding and removal of elements, but not their properties. Those properties should be tracked by the single view model.
5. (optional)Decide for yourself whether you want to use commands or to handle the events on button clicks and events from observable collection. Use only one approach, I would suggest the first one.
6. In window constructor try to bind DataContext to the view model for all node details. Then change the binding properties accordingly. ItemsSource bind to Students seems fine, so verify the other bindings, those for single node view model. Make sure it works fine.
Then you can try your original way of setting the context via Access method.
Fazil13 17-Jan-21 1:51am
Yes through the button i can able to set the data working properly,but when i try to change the data from the other control view model then it fails i think calling method i am doing wrong
I see. There are few approaches and I would suggest starting from the simplest one. You have two controls and two view models, a view model for each UI control. Let's say you have a window and you load one of the controls in it, for example StudentsView, set its view model -bigStudentViewModel, and do some stuffs, then you load the other - CourcesView,one etc. Once you load the second control, the first one and it's view model are to be disposed (I mean marked for Garbage collection) So, first question - can those two view models be combined into one? So, you will still have StudentsView and CourcesView, but you will have one big view model, BigStudentCoursesViewModel. This big view model will always be bound to window and you will change only the view, or you can have both views in one window talking to the same view model. In such case updates will work. Even if you add Teachers and Class Rooms etc etc. the big view model will hold. But if you continue to add more and more stuffs, at one point it will become too big and clumsy. So you have to think of other means to do the job :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900