I'm designing a VB .NET WPF MVVM application which has a Public Shared Property declared on the main window viewmodel as follows:
Shared _CurrentUser As New CurrentUser Public Shared Property CurrentUser As CurrentUser Get Return _CurrentUser End Get Set(value As CurrentUser) _CurrentUser = value End Set End Property
I'm utilizing it as a Shared property because it is used by pretty much every other viewmodel in my application. I'm not going to post the whole Current User class because it gets several layers deep and I don't think it's necessary to understand the problem. To illustrate... here are two buttons I currently have on my main application window:
<Button Content="{Binding Path=CurrentUser.Employee.WorkItemBase.MyWorkButtonLabel}" Height="24" Command="{Binding OpenUserWorkItems}"/><Button Content="{Binding TestWrapper}" Height="24" Command="{Binding OpenUserWorkItems}"/>
And here is TestWrapper:
Public ReadOnly Property TestWrapper() As String Get Return _CurrentUser.Employee.WorkItemBase.MyWorkButtonLabel End Get End Property
Both of my buttons load correctly, but the problem is that I've now implemented a timer which runs the following code:
'Attempt to refresh Work Items intRefreshWorkItemsReturn = _CurrentUser.Employee.WorkItemBase.RefreshAllWorkItems(exRefreshingWorkItems, _ _CurrentUser.Employee) 'Check return value If intRefreshWorkItemsReturn = 0 Then 'Error occurred... I want the timer to log it silently 'Log error 'exRefreshingWorkItems.LogError() End If Call MsgBox("Work Items Refreshed.") 'Call NotifyPropertyChanged for CurrentUser Call NotifyPropertyChanged("CurrentUser") Call NotifyPropertyChanged("CurrentUser.Employee.WorkItemBase.MyWorkButtonLabel") Call NotifyPropertyChanged("TestWrapper")
Now by my last three lines of code you might be able to guess what my issue is. When i run my app, then do something which will change the value of "MyWorkButtonLabel" when RefreshAllWorkItems is called, the button bound to "TestWrapper" updates its content fine with the timer code while the button bound to "CurrentUser.Employee.WorkItemBase.MyWorkButtonLabel" does not.
I'm pretty new to this, so maybe I've already revealed the best solution for a novice like me... wrap the "sub property" in a new property on my main window viewModel and explicitly call "NotifyPropertyChanged" when I need to. This isn't the end of the world if I have to do it that way, but I can't help thinking that I'm missing an easy solution to get CurrentUser to update the Content of my button.
Thanks for the help!!!