Click here to Skip to main content
15,891,136 members

View Models and View in WPF

IamBlessed asked:

Open original thread
Hi guys I have a wpf application that is using the mvvm pattern, in my application I have two view model, One controls my main window and the other control my page. I want to be able to bind my login page UI element to my LoginViewModel? Any idea on what to do thanks below is my code for my two view model

Main Window view model
C#
#region Displayed page
       private Uri displayedPage;

       public Uri DisplayedPage
       {
           get
           {
               return this.displayedPage;
           }
           set
           {
               this.displayedPage = value;
               base.OnPropertyChanged("DisplayedPage");
           }
       }

       #endregion
       #endregion

       #region Constructor
       public MainWindowViewModel()
       {
           LoginViewModel _login = new LoginViewModel(this);
       }
       #endregion


while the view model for my page
C#
#region Constructor
        public LoginViewModel(MainWindowViewModel _mainwin)
        {
            _mainViewModel = _mainwin;
            _mainViewModel.DisplayedPage = new Uri("/View/AuthenticationView/LoginPage.xaml", UriKind.RelativeOrAbsolute);
        }
        #endregion


and this is the code for my page
HTML
<TextBlock Grid.Row="2" HorizontalAlignment="Left" Name="UsrNmTxtBlk" Text="Username :" VerticalAlignment="Top" Padding="3" Margin="3,0,0,0" Grid.ColumnSpan="2" />
        <TextBox Grid.Row="3" Height="40" Name="UserNmTxtBx" Margin="3" Grid.ColumnSpan="2" VerticalContentAlignment="Center" Text="{Binding Path=OperatorUserID}"/>
        <TextBlock Grid.Row="4" HorizontalAlignment="Left" Name="UsrPassBlk" Text="Password :" VerticalAlignment="Top" Padding="3" Grid.ColumnSpan="2" />
        <PasswordBox Grid.Row="5" Height="40" HorizontalAlignment="Stretch" Name="UserPassTxtbx" Margin="3" Grid.ColumnSpan="2" VerticalContentAlignment="Center" 
                     passhelper:PasswordBxHelper.BindPassword="True"
                     passhelper:PasswordBxHelper.BoundPassword="{Binding Path=OperatorPassword}"/>
        <Button Content="Login" Grid.Column="1" Grid.Row="6" Height="40" 
                HorizontalAlignment="Right" Margin="3" Name="LogInBtn" 
                Width="78" Grid.ColumnSpan="2" />
Tags: C# (C# 4.0), WPF, MVVM

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900