仅在 WPF MVVM 中的两个视图之间进行通信

发布于 2024-11-26 06:02:30 字数 2750 浏览 0 评论 0原文

我在 MVVM(WPF) 中有两个视图。第一个视图包含两个文本框:用户名、密码,第二个视图有两个按钮:提交和清除。两个视图现在都设置在表单上。当我按“清除”按钮时,两个文本框都会被清除,并在“提交”中显示用户名和密码的消息。我只使用 MVVM+WPF,没有使用 prism。

第一个视图的 ModelView:

class LoginView:ViewModelBase
    {
        string _userName;
        public string  UserName 
        {
            get {return _userName ; }
            set {
                if (_userName != value)
                {
                    _userName = value;
                }
                base.OnPropertyChanged(UserName);
            }
        }

        string _Pwd;
        public string PWD
        {
            get { return _Pwd; }
            set
            {

                _Pwd = value;
                base.OnPropertyChanged(_Pwd);
            }
        }
    }

的按钮

 class ButtonHandler
    {
        private DelegateCommand _ClearData;
        public ICommand ClearCommand
        {
            get
            {
                if (_ClearData == null)
                {
                    _ClearData = new DelegateCommand(ClearText);
                }
                return _ClearData;
            }
        }
        LoginView lg = new LoginView();
        private void ClearText()
        {
            lg.UserName = "";
            lg.PWD = "";
        }
    }

以及第一个控件和

   <Label Content="Login" Grid.Row="0" Grid.Column="1" VerticalAlignment="Top" HorizontalAlignment="Left"
           FontFamily="Georgia" FontSize="24" FontWeight="UltraBold" ></Label>
    <Label Content="User Name" Grid.Row="1" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left"></Label>
    <Label Content="Password" Grid.Row="2" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left"></Label>
    <TextBox  Name="username" Grid.Row="1" Grid.Column="1" Margin="100,0,0,0" VerticalAlignment="Center" HorizontalAlignment="Left" Text="{Binding Path=UserName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" ></TextBox>
    <TextBox  Name="pwd" Grid.Row="2" Grid.Column="1" Margin="100,0,0,0" VerticalAlignment="Center" HorizontalAlignment="Left" Text="{Binding Path=PWD,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></TextBox>
    <Separator Grid.Row="0" Grid.Column="1" Height="5" Margin="0,40,0,0" Background="Green"></Separator>

按钮视图

     <Button x:Name="Submit" Content="Submit" Grid.Column="1"></Button>
     <Button x:Name="Clear" Content="Clear" Grid.Column="2" 
             Command="{Binding Path=ClearCommand, Mode=OneWay, 
             UpdateSourceTrigger=PropertyChanged}" >
     </Button>

和视图代码为什么它不起作用?

I Have two views in MVVM(WPF). First View contains two Text boxes: User Name, Password, second view is having two Buttons: Submit and Clear. Both Views now set on On Form. When I press 'Clear' button both textboxes are cleared and in Submit a message of UserName and Password is displayed. I am using only MVVM+WPF, not prism.

ModelView Of First View:

class LoginView:ViewModelBase
    {
        string _userName;
        public string  UserName 
        {
            get {return _userName ; }
            set {
                if (_userName != value)
                {
                    _userName = value;
                }
                base.OnPropertyChanged(UserName);
            }
        }

        string _Pwd;
        public string PWD
        {
            get { return _Pwd; }
            set
            {

                _Pwd = value;
                base.OnPropertyChanged(_Pwd);
            }
        }
    }

and For Button

 class ButtonHandler
    {
        private DelegateCommand _ClearData;
        public ICommand ClearCommand
        {
            get
            {
                if (_ClearData == null)
                {
                    _ClearData = new DelegateCommand(ClearText);
                }
                return _ClearData;
            }
        }
        LoginView lg = new LoginView();
        private void ClearText()
        {
            lg.UserName = "";
            lg.PWD = "";
        }
    }

and View Code of First Control

   <Label Content="Login" Grid.Row="0" Grid.Column="1" VerticalAlignment="Top" HorizontalAlignment="Left"
           FontFamily="Georgia" FontSize="24" FontWeight="UltraBold" ></Label>
    <Label Content="User Name" Grid.Row="1" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left"></Label>
    <Label Content="Password" Grid.Row="2" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left"></Label>
    <TextBox  Name="username" Grid.Row="1" Grid.Column="1" Margin="100,0,0,0" VerticalAlignment="Center" HorizontalAlignment="Left" Text="{Binding Path=UserName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" ></TextBox>
    <TextBox  Name="pwd" Grid.Row="2" Grid.Column="1" Margin="100,0,0,0" VerticalAlignment="Center" HorizontalAlignment="Left" Text="{Binding Path=PWD,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></TextBox>
    <Separator Grid.Row="0" Grid.Column="1" Height="5" Margin="0,40,0,0" Background="Green"></Separator>

and Button View

     <Button x:Name="Submit" Content="Submit" Grid.Column="1"></Button>
     <Button x:Name="Clear" Content="Clear" Grid.Column="2" 
             Command="{Binding Path=ClearCommand, Mode=OneWay, 
             UpdateSourceTrigger=PropertyChanged}" >
     </Button>

Why it is not working?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一人独醉 2024-12-03 06:02:30

您没有正确使用 MVVM 模式,使用此模式,ViewModel 不应该引用视图。命令是 ViewModel 的一部分,因此您对 LoginView 的引用违反了该模式。

那么你有两个输入字段和一个按钮?为此,我将有一个 ViewModel 和一个 View。 ViewModel 将公开两个字符串属性(用户名和密码)和一个绑定到清除按钮的命令。当命令执行时,它将清除 ViewModel 上的用户名和密码文本。然后视图将相应更新。

You are not using the MVVM pattern correctly, with this pattern the ViewModel should not have a reference to the View. A command is part of your ViewModel, therefore your reference to LoginView violates the pattern.

So you have two input fields and a button? for this I would have a single ViewModel and a single View. The ViewModel would expose two string properties (username & password) and a command that binds to the clear button. When the command executes it would clear the username and password texts on the ViewModel. The View will then update accordingly.

没有伤那来痛 2024-12-03 06:02:30

MVVM 的基本原则是拥有一个视图可以绑定到的类,该类内部包含所有应用程序逻辑。主要原因之一是要关注点分离。因此,如果您想要一个用户名,您可以公开视图绑定到的属性,然后当您想要登录时,您创建一个函数,该函数使用这些绑定值提交给您应用程序的业务逻辑层。

这似乎是在您的示例中使用 MVVM 的一种方法:

public class LoginViewModel
{
  public string UserName {get;set;}//Implement INotifyPropertyChanged
  public string PWD {get;set;}

    private DelegateCommand _ClearData;
    public ICommand ClearCommand
    {
        get
        {
            if (_ClearData == null)
            {
                _ClearData = new DelegateCommand(ClearText);
            }
            return _ClearData;
        }
    }

    private void ClearText()
    {
        UserName = "";
        PWD = "";
    }
}

然后在您的 xaml 中:

<TextBox Text={Binding UserName} />
<TextBox Text={Binding PWD} />
<Button Command={Binding ClearCommand}/>

The basic principle of MVVM is to have a class that the view can bind to that has all the application logic inside of it. One of the main reasons is to have a separation of concerns. So if you want a username you expose a property that the view binds to and then when you want to log in you create a function that uses those bound values to submit to you business logic layer of your application.

This would seem to be one way to utilize MVVM in your example:

public class LoginViewModel
{
  public string UserName {get;set;}//Implement INotifyPropertyChanged
  public string PWD {get;set;}

    private DelegateCommand _ClearData;
    public ICommand ClearCommand
    {
        get
        {
            if (_ClearData == null)
            {
                _ClearData = new DelegateCommand(ClearText);
            }
            return _ClearData;
        }
    }

    private void ClearText()
    {
        UserName = "";
        PWD = "";
    }
}

and then in your xaml:

<TextBox Text={Binding UserName} />
<TextBox Text={Binding PWD} />
<Button Command={Binding ClearCommand}/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文