im having a problem with my WPF application. I need to communicate through different ViewModels and i dont know exactly how. I dont want also to use a Framework, i have found the Mediator pattern and i have used it as a help class and it worked i called a method from an other Class but i dont know if is the perfect way to communicate with other ViewModels. Now my question really is how i can populate a combobox from a View1 with the selected item from it to my View2, ofc i have different ViewModels for each one and i want to saw the same on the two of them. For example : HERE! . I know that my ViewModels must only give orders on their Views and i think that i can also do that with the use of Mediator but i dont really know if this is a really good idea or there is a better way and exactly how i can do it. I have lost a week searching over what would be better to do, so i want a solution that would be the best and not against the MVVM pattern. Thanks in advance.
<ComboBox VerticalAlignment="Center" Grid.Column="1" Grid.Row="0" IsEditable="False" IsSynchronizedWithCurrentItem="false" ItemsSource="{Binding combobox}" SelectedItem="{Binding selectedEmployee}" ItemTemplate="{StaticResource myResources}" />
MyViewModel
public ObservableCollection<ViewModelEmp> combobox
{
get { return this._combobox; }
set
{
this._combobox = value;
OnPropertyChanged("combobox");
}
}
I Use an ObserveableCollection in my class ViewModelEmp and i fill my combobox with a method from my database and then i'm calling them on my constructor:
public ViewModelEmp()
{
this._combobox = new ObservableCollection<ViewModelEmp>();
this.databaseconnect();
}
And like this i'm viewing on my combobox my Data from my Database, know if i want to show the same in an other View and ViewModel what exactly i can do?
You could expose an event on your mediator so it notifies all listeners (eg your second VM) that its values have changed. .NET has a standard way of doing this: you could implement the INotifyPropertyChanged
interface:
http://msdn.microsoft.com/en-us/library/vstudio/system.componentmodel.inotifypropertychanged
An example/tutorial: http://www.dreamincode.net/forums/topic/208833-using-the-inotifypropertychanged-functionality/
The easiest way would be to use a messenger implementation, please
read this answer: https://stackoverflow.com/a/16149579/690178
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.