我的MainWindow加载了新的UserControls,ViewModel加入了它的ContentControl,所以视图被切换了。
但是,我需要从ContentControl中的ViewModel访问我的MainWindow ViewModel中的属性。
MainWindowViewModel
class_num = script.quiz()
def path(class_num):
下面你可以看到MainWindow ContentControl中的LoginViewModel,我添加了一条评论,我试图将这个新用户添加到ObservableCollection中。
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!");
}
}
}
修改: 这是我在MainWindowViewModel
中添加视图的地方#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
答案 0 :(得分:0)
我会使用ViewModel-to-ViewModel通信的事件,我更喜欢IEventAggregator,它可以作为Microsoft的PubSub nuget包提供,但有很多可供选择(或者根据自己的喜好自行推送)。
public MainViewModel() {
Aggregator.GetEvent<UserLoggedInEvent>().Subscribe(user => ...do your magic);
}
在您的LoginViewModel中,在用户登录后发布:
public DoLogin() {
... do other stuff here...
Aggregator.GetEvent<UserLoggedInEvent>().Publish(userDetails);
}
使用Prism的IEventAggregator,事件类很简单:
public class UserLoggedInEvent : PubSubEvent<User> {}
Btw - MVVM或任何设计模式的主要目的之一是从业务代码中抽象UI,因此如果您可以使用转换器或其他东西从VM中删除所有App.Current.Resources内容,那么您已经从WPF中抽象出来(更容易移植到其他平台,如UWP)。
答案 1 :(得分:0)
You simply need to use glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
binding from within Ancestor
any child element :
ContentControl's
If {Binding RelativeSource={RelativeSource AncestorType={x:Type Window},Path=DataContext.AnyPropertyOfMainWindowViewModel}
has Window
as MainWindowViewModel
.