I have a WPF Mvvm-Light application that uses tab controls to display different views to the user.
PROBLEM:
THe problem I have is that when the program first loads the tab control displays empty. But when debugging locally this problem never occurs. This application has been in development for over a year without this issue and all of a sudden it started happening. see below image
HACK FIX:
If the user resizes their window, the content in the tab control refreshes and appears and the problem never reoccurs while they have the application open. But this issue occurs 100% of the time on first program load and first tab load (which happens at the same time)
DETAILS:
here is how I define my views that get inserted into the tab control
<DataTemplate DataType="{x:Type FtcViewModel:DashboardNavViewModel}">
<FtcView:DashboardNav />
</DataTemplate>
and here is my tab control:
<TabControl Grid.Column="1" Grid.Row="3"
SelectedItem="{Binding CurrentViewModel}"
ItemsSource="{Binding OpenViewModelCollection}"
Style="{StaticResource TabControlStyle}"
ItemContainerStyle="{StaticResource TabItemStyle}" >
</TabControl>
then in my view model for the main window i assign a collection to the tab control and by default set the dashbaord as the first tab to open like this (tried to only include relevant code, obiously there is more in the viewModel file):
Public Property OpenViewModelCollection As ObservableCollection(Of ViewModelHelper)
Get
Return Me._OpenViewModelCollection
End Get
Set(value As ObservableCollection(Of ViewModelHelper))
If _OpenViewModelCollection Is value Then
Return
End If
_OpenViewModelCollection = value
RaisePropertyChanged(OpenViewModelCollectionPropertyName)
End Set
End Property
Public Property CurrentViewModel As ViewModelHelper
Get
Return Me._CurrentViewModel
End Get
Set(value As ViewModelHelper)
If _CurrentViewModel Is value Then
Return
End If
''if change of viewmodel is not from OpenTabViewModelCommandExecute method
If FlagOpening = False AndAlso value IsNot Nothing Then
If _CurrentViewModel IsNot Nothing Then
_CurrentViewModel.HandleNavigation(True)
End If
''Mark NavService target VM
_NavService.TargetViewModelKey = value.vmKey
''evaluate if naviagtion has been canceled
If _NavService.bCanNavigate = False Then
_NavService.bCanNavigate = True
Exit Property
End If
End If
''if navigation not canceled, finish assigning new view model
_CurrentViewModel = value
RaisePropertyChanged(CurrentViewModelPropertyName)
If _CurrentViewModel IsNot Nothing Then
_CurrentViewModel.RefreshModel()
End If
End Set
End Property
...
'' THIS CODE IS FROM THE CONSTRUCTOR OF THE VIEW MODEL CLASS
OpenViewModelCollection.Add(_Locator.DashboardHome_VM)
CurrentViewModel = OpenViewModelCollection(0)
QUESTION
can someone help me figure out why this is happening or suggest a way to refresh the UI after the first application load.
thanks in advance
UPDATE 1 - Oct 4, 2017
I have been able to reproduce this problem on my development machine. When the UI is blank you can see that the XAML diagnostics tools don't show up. THe image below shows the same app being debugged, but the one without the XAML tools box is the one with the missing ui elements.
The following content is not being rendered inside my custom tab control style:
<ScrollViewer VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Auto" >
<ContentPresenter x:Name="PART_SelectedContentHost" ContentSource="SelectedContent" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</ScrollViewer>
EDIT 2 Oct 11 2017
I have set breakpoints in both the xaml and code in the ViewModel. I can confirm that the ViewModel data is correctly initialized and assigned to the observable objects. I also inserted "test" textblocks in my tabItemTemplate to confirm which part is not showing. Right now I have a terrible hack of resizing the window from code on application startup, while that works, I still would like to figure out what is going on.
SelectedItem="{Binding CurrentViewModel}"
andItemsSource="{Binding OpenViewModelCollection}"
? – Matisse