Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

.Net MVVM accessing property of "parent" ViewModel

$
0
0

Hello!

I'm building my first vb.net/wpf/mvvm application and have run into a small snag that I can't seem to find an answer for on the internet.  My application launches viewMainWindow with viewModelMainWindow as the DataContext.  Here is my viewModelMainWindow constructor:

        Public Sub New()

            'Declare Variables
            Dim wsTimeClock As New Workspace
            Dim vwTimeClock As New viewTimeClock
            Dim collWorkspaces As New ObservableCollection(Of Workspace)
            Dim objCurrentUser As New CurrentUser
            Dim objSQLServer As New SQLServer

            'Check for User Permissions to SQL Server
            If objSQLServer.HasSQLServerLogin(objCurrentUser) = False Then

                'Create the Login
                objSQLServer.CreateSQLServerLogin(objCurrentUser)

            End If

            'Check for database role
            If objSQLServer.HasDatabaseRole(objCurrentUser, "DB_StandardUser") = False Then

                'Grant DB_StandardUser Role
                objSQLServer.AddStandardUserRole(objCurrentUser)

            End If

            'Check for Employee Record
            If objCurrentUser.HasEmployeeRecord() = False Then

                'Create an Employee Record
                objCurrentUser.CreateEmployeeRecord()

            End If

            'Set the Public CurrentUser property to objCurrentUser
            Me.CurrentUser = objCurrentUser
            Call MsgBox(Me.CurrentUser.EmployeeID)

            'Set Values for wsTimeClock
            wsTimeClock.ViewModelName = "vmTimeClock"
            wsTimeClock.tabName = "Time Clock"
            wsTimeClock.ViewObject = vwTimeClock

            'Add workspace to collection
            collWorkspaces.Add(wsTimeClock)

            'Set Public Property Workspaces to collWorkspaces
            Me.Workspaces = collWorkspaces

            'Clean Up
            objCurrentUser = Nothing
            objSQLServer = Nothing

        End Sub

The msgbox is to show that, in fact, this code is working as I expect and the correct EmployeeID (2 in this case) is returned (HasEmployeeRecord() and CreateEmployeeRecord() both set EmployeeID in my CurrentUser class).  The problem I'm having is that I need my "child" ViewModels to be able to access the CurrentUser public property of viewModelMainWindow:

        Public Property CurrentUser As CurrentUser
            Get
                Return _CurrentUser
            End Get
            Set(value As CurrentUser)
                _CurrentUser = value
            End Set
        End Property

Here is a snippet from a child ViewModel called ViewModelTimeClock:

                    Dim timeRecord As New TimeRecord
                    Dim objCurrentUser As New CurrentUser

                    'Set values
                    timeRecord.EmployeeID = objCurrentUser.EmployeeID
                    timeRecord.ClockInTime = _NewClockInTime
                    timeRecord.ClockOutTime = _NewClockOutTime
                    timeRecord.Notes = _NewNotes

                    'Commit record to database
                    timeRecord.CommitTimeRecordToDB(objCurrentUser)


This actually doesn't generate any error message and adds the record to the DB, but the problem is that since I never call HasEmployeeRecord() or CreateEmployeeRecord(), EmployeeID is never set properly.  Obviously, I could call one to get it set, but I would prefer to only have to retrieve EmployeeID once and then access it from all child ViewModels rather than instantiating a CurrentUser object over and over again.  From my research, I know that I could probably get this done by adding a "CurrentUser" parameter to each child ViewModel constructor, but I can't figure out how to pass parameters with my current implementation of assigning the ViewModel in my XAML for ViewTimeClock:

<UserControl.DataContext><VM:viewModelTimeClock/></UserControl.DataContext>

Any tips about how to handle this would be greatly appreciated!!

Thanks in advance,

Kris


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>