I have got my MainWindow which loads new UserControls and there ViewModel's into it's ContentControl, so the Views are switched.
However, I need to access a property in my MainWindow ViewModel from a ViewModel within the ContentControl.
MainWindowViewModel
namespace PhotoManagement
{
public class MainWindowViewModel : NotifyUIBase
{
public ObservableCollection<ViewVM> Views { get; set; }
private ObservableCollection<Logged> loggedUsers;
public ObservableCollection<Logged> LoggedUsers
{
get
{
return loggedUsers;
}
set
{
loggedUsers.Add(value[0]);
//There is a user logged in, switch to home and display menu
if (loggedUsers.Count > 0)
{
//Display menu, switch Windows
MessageBox.Show("Someone is logged in!");
}
else
{
MessageBox.Show("No-one is logged in!");
}
}
}
Below you can see the LoginViewModel which is in the MainWindow ContentControl, I have added a comment where i'm trying to add this new user to the ObservableCollection.
#region Login Methods
private LoginVM loginVM;
public LoginVM LoginVM
{
get
{
return loginVM;
}
set
{
loginVM = value;
editEntity = editVM.TheEntity;
RaisePropertyChanged();
}
}
protected override void DoLogin()
{
//Check if email exists
var exist = db.Users.Count(a => a.Email == LoginVM.TheEntity.Email);
if (exist != 0)
{
//Fecth user details
var query = db.Users.First(a => a.Email == LoginVM.TheEntity.Email);
if (Common.Security.HashGenerator.CalculateHash(LoginVM.TheEntity.ClearPassword, query.Salt) == query.Hash)
{
//Password is correct
MessageBox.Show("Details correct!");
//Set properties
LoginVM.TheEntity.FirstName = query.FirstName;
LoginVM.TheEntity.LastName = query.LastName;
LoginVM.TheEntity.UID = query.UID;
//Add the LoginVM to LoggedUsers
Edit: This is where I add the Views in MainWindowViewModel
namespace PhotoManagement
{
public class MainWindowViewModel : NotifyUIBase
{
public ObservableCollection<ViewVM> Views { get; set; }
private ObservableCollection<Logged> loggedUsers;
public ObservableCollection<Logged> LoggedUsers
{
get
{
return loggedUsers;
}
set
{
loggedUsers.Add(value[0]);
//There is a user logged in, switch to home and display menu
if (loggedUsers.Count > 0)
{
//Display menu, switch Windows
MessageBox.Show("Someone is logged in!");
}
else
{
MessageBox.Show("No-one is logged in!");
}
}
}
public string Version
{
get { return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(); }
}
public MainWindowViewModel()
{
ObservableCollection<ViewVM> views = new ObservableCollection<ViewVM>
{
new ViewVM { IconGeometry=App.Current.Resources["home4"] as Geometry, ViewDisplay="Home", ViewType = typeof(LoginView), ViewModelType = typeof(LoginViewModel)},
new ViewVM { IconGeometry=App.Current.Resources["instagram3"] as Geometry, ViewDisplay="Images", ViewType = typeof(LoginView), ViewModelType = typeof(LoginView)},
new ViewVM { IconGeometry=App.Current.Resources["money674"] as Geometry, ViewDisplay="Sales", ViewType = typeof(LoginView), ViewModelType = typeof(LoginViewModel)},
new ViewVM { IconGeometry=App.Current.Resources["printing1"] as Geometry, ViewDisplay="Print Queue", ViewType = typeof(LoginView), ViewModelType = typeof(LoginViewModel)},
new ViewVM { IconGeometry=App.Current.Resources["cog2"] as Geometry, ViewDisplay="Settings", ViewType = typeof(IconLibaryView), ViewModelType = typeof(IconLibaryViewModel)},
new ViewVM { IconGeometry=App.Current.Resources["upload40"] as Geometry, ViewDisplay="Upload", ViewType = typeof(IconLibaryView), ViewModelType = typeof(IconLibaryViewModel)}
};
Views = views;
RaisePropertyChanged("Views");
views[0].NavigateExecute();
}
}
}
MainWindowViewModel
creates theLoginVM
to add to theViews
collection, it should give it a property or delegate so it can set the currently logged in user. The alternative ifMainWindowViewModel
andLoginVM
are completely unlinked would be to use a messaging system, such as MVVM Light'sMessenger
or Microsoft Prism'sEventAggregator
. – RachelMessenger
before, will have to have a look into it. – Martyn Ball