Click here to Skip to main content
15,029,206 members
Please Sign up or sign in to vote.
0.00/5 (No votes)

I have been searching around without finding a fix so here I go.

I have a Windows with a center that contain a UserControl that will fill the general area of my application.

I have a TabControl with multiple TabItem. In each TabItem I have to show different controls including Datagrids.

Here is the sample code of my second TabItem.
<TabItem Header="Suivi" IsSelected="True">
           <Grid Background="#FFE5E5E5" >
               <DataGrid x:Name="dgSuivi" ItemsSource="{Binding Source=Suivi}" AutoGenerateColumns="False" CanUserAddRows="False"  >
                       <DataGridTextColumn Header="Suivi" Binding="{Binding COD_NOM }" Width="500" />
                       <DataGridTextColumn Header="Date planifiée" Binding="{Binding DAT_PLAN}" Width="150" />
                       <DataGridTextColumn Header="Date révisée" Binding="{Binding DAT_REVIS}" Width="150" />
                       <DataGridTextColumn Header="Date réelle" Binding="{Binding DAT_REEL}" Width="150" />

My code behind has a filled property called Suivi.

Public Property Suivi As ObservableCollection(Of MyType)

and MyType is the following class:

Public Class EntiteSgcrSuivi

     Property COD_NOM as String

     Property DAT_PLAN as DateTime

     Property DAT_REVIS as DateTime

     Property DAT_REEL as DateTime

     Property DAT_RAPPEL as DateTime

     Public Sub New()

      COD_NOM_DAT = Nothing
      DAT_PLAN = New System.DateTime(9999, 1, 1)
      DAT_REVIS = New System.DateTime(9999, 1, 1)
      DAT_REEL = New System.DateTime(9999, 1, 1)

     End Sub

    End Class

When I change to the second TabItem (Suivi) the datagrid is filled with empty lines.

[Picture of the datagrid not being filled](

I've been searching to fix this but I think I am missing a notion here.
Is my binding done right?
Updated 22-Jul-20 22:25pm

1 solution

I had the same problem and here is my solution:

- Move your DataGrids to the tabcontrol resource as tiggers.

        <Style x:Key="Tab1" TargetType="TabItem">
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Content">
                            <DataGrid Background="#FFF" ItemsSource="{Binding MyListForTab1}" />

        <Style x:Key="Tab2" TargetType="TabItem">
            <Setter Property="Padding" Value="16 6" />
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Content">
                            <DataGrid Background="#FFF" ItemsSource="{Binding MyListForTab2}" />

    <TabItem Style="{StaticResource Tab1}" Header="Tab1" />

    <TabItem Style="{StaticResource Tab2}" Header="Tab2" />


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

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