Mvvm 从视图模型到视图的通信

Mvvm 从视图模型到视图的通信,mvvm,properties,bind,Mvvm,Properties,Bind,在这种情况下,我需要从一个视图模型到另一个视图进行通信,以防发生更改。这里的问题是,我想要更新的属性没有DependencyProperty,我已经设法通过打破一些MVVM模式规则并简单地在事件后面添加代码,将它从视图绑定到VM: (ViewModelSomething as DataContext).Password = pb.Password; 这是有效的。问题是,当VM中发生更改时,我希望更新视图的此属性。甚至可能存在这样的连接吗?在ViewModel上实现INotifyProperty

在这种情况下,我需要从一个视图模型到另一个视图进行通信,以防发生更改。这里的问题是,我想要更新的属性没有DependencyProperty,我已经设法通过打破一些MVVM模式规则并简单地在事件后面添加代码,将它从视图绑定到VM:

(ViewModelSomething as DataContext).Password = pb.Password;

这是有效的。问题是,当VM中发生更改时,我希望更新视图的此属性。甚至可能存在这样的连接吗?

在ViewModel上实现INotifyPropertyChanged

在ViewModel上实现INotifyPropertyChanged

在ViewModel上实现INotifyPropertyChanged

在ViewModel上实现INotifyPropertyChanged

您可以创建一个
GetCredential()
方法,您可以从ViewModel中的该方法获取更新的值

实施将是这样的

View.cs

public Credential GetCredential()
    {
        var credential = new Credential()
        {
            Username = txtUserName.Text,
            Password = txtPassword.Password
        };
        return credential;
    }
您需要一个凭证

 public class Credential
    {
        public string Username { get; set; }
        public string Password { get; set; }
    }
ViewModel.cs

public Credential GetCredential()
    {
        var credential = new Credential()
        {
            Username = txtUserName.Text,
            Password = txtPassword.Password
        };
        return credential;
    }
在Submit按钮命令处理程序中,您可以从视图中获取值

 Credential credential = View.GetCredential();

您可以在登录视图中创建一个
GetCredential()
方法,并且可以从ViewModel中的该方法获取更新的值

实施将是这样的

View.cs

public Credential GetCredential()
    {
        var credential = new Credential()
        {
            Username = txtUserName.Text,
            Password = txtPassword.Password
        };
        return credential;
    }
您需要一个凭证

 public class Credential
    {
        public string Username { get; set; }
        public string Password { get; set; }
    }
ViewModel.cs

public Credential GetCredential()
    {
        var credential = new Credential()
        {
            Username = txtUserName.Text,
            Password = txtPassword.Password
        };
        return credential;
    }
在Submit按钮命令处理程序中,您可以从视图中获取值

 Credential credential = View.GetCredential();

您可以在登录视图中创建一个
GetCredential()
方法,并且可以从ViewModel中的该方法获取更新的值

实施将是这样的

View.cs

public Credential GetCredential()
    {
        var credential = new Credential()
        {
            Username = txtUserName.Text,
            Password = txtPassword.Password
        };
        return credential;
    }
您需要一个凭证

 public class Credential
    {
        public string Username { get; set; }
        public string Password { get; set; }
    }
ViewModel.cs

public Credential GetCredential()
    {
        var credential = new Credential()
        {
            Username = txtUserName.Text,
            Password = txtPassword.Password
        };
        return credential;
    }
在Submit按钮命令处理程序中,您可以从视图中获取值

 Credential credential = View.GetCredential();

您可以在登录视图中创建一个
GetCredential()
方法,并且可以从ViewModel中的该方法获取更新的值

实施将是这样的

View.cs

public Credential GetCredential()
    {
        var credential = new Credential()
        {
            Username = txtUserName.Text,
            Password = txtPassword.Password
        };
        return credential;
    }
您需要一个凭证

 public class Credential
    {
        public string Username { get; set; }
        public string Password { get; set; }
    }
ViewModel.cs

public Credential GetCredential()
    {
        var credential = new Credential()
        {
            Username = txtUserName.Text,
            Password = txtPassword.Password
        };
        return credential;
    }
在Submit按钮命令处理程序中,您可以从视图中获取值

 Credential credential = View.GetCredential();


这已经实现了,问题是属性不知道在哪里绑定它的值,因为视图中没有绑定问题是PasswordBox。Password不是依赖性属性,所以它不能绑定…我知道它不能绑定,否则我将使用标准绑定。。。我在问是否有办法。这已经实现了,问题是属性不知道在哪里绑定它的值,因为视图中没有绑定问题是PasswordBox。Password不是依赖性属性,所以它不能绑定…我知道它不能绑定,否则我只会使用标准绑定。。。我在问是否有办法。这已经实现了,问题是属性不知道在哪里绑定它的值,因为视图中没有绑定问题是PasswordBox。Password不是依赖性属性,所以它不能绑定…我知道它不能绑定,否则我只会使用标准绑定。。。我在问是否有办法。这已经实现了,问题是属性不知道在哪里绑定它的值,因为视图中没有绑定问题是PasswordBox。Password不是依赖性属性,所以它不能绑定…我知道它不能绑定,否则我只会使用标准绑定。。。我在问是否有办法。这里有一个类似的问题:。不过,您可能不会喜欢接受的答案…为什么在没有用户交互的情况下,可以在viewmodel中更改密码?这里有一个类似的问题:。不过,您可能不会喜欢接受的答案…为什么在没有用户交互的情况下,可以在viewmodel中更改密码?这里有一个类似的问题:。不过,您可能不会喜欢接受的答案…为什么在没有用户交互的情况下,可以在viewmodel中更改密码?这里有一个类似的问题:。不过,您可能不会喜欢接受的答案……为什么在没有用户交互的情况下,可以在viewmodel中更改密码?