Click here to Skip to main content
11,476,994 members (72,593 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: WPF C#4.0 MVVM view
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
 #region Displayed page
        private Uri displayedPage;
        public Uri DisplayedPage
                return this.displayedPage;
                this.displayedPage = value;
        #region Constructor
        public MainWindowViewModel()
            LoginViewModel _login = new LoginViewModel(this);

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

and this is the code for my page
<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.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" />
Posted 27-Feb-13 11:54am
Lance Contreras at 26-Mar-13 11:14am
I don't get it, what's the structure of your UI? How are you going to launch the login page from the main page? You can try binding it explicitly by setting the control's DataContext to a specific viewmodel object (i.e. MainPage.DataContext = new MainPageViewModel() or LoginPage.DataContext = new LoginViewModel()). It really depend's on the structure of your code.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

By no means is this a short solution, but I guarantee if you go through these two tutorials you will have your answer:[^][^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 274
1 Richard Deeming 245
2 Andy Lanng 185
3 _duDE_ 175
4 Sascha Lefèvre 150
0 Sergey Alexandrovich Kryukov 7,675
1 OriginalGriff 7,246
2 Sascha Lefèvre 3,034
3 Maciej Los 2,491
4 Richard Deeming 2,305

Advertise | Privacy | Mobile
Web03 | 2.8.150520.1 | Last Updated 1 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100