Search code examples
c#wpftabcontrol

How to reinitialize a custom control on a Tab Control page when the page is selected


In the following code, I am placing custom controls on each page of a TabControl. When first loading the TabControl, all the custom controls are initialized and layed out.

All the custom controls can update the source database.

How do I reinitialize each custom control when the tab page it is on is selected? (i.e., The MedicalRecords custom control will update the database, to which the Nursing custom control will have to reinitialize, etc.).

TIA

<TabControl>
        <TabItem x:Name="AppointmentsTab" Header="Appointments and Scheduling">
            .........      
        </TabItem>
        <TabItem x:Name="MedicalRecordsTab" Header="Medical Records">
            <mr:MedicalRecords/>
        </TabItem>
        <TabItem x:Name="NursingTab" Header="Nursing Service">
            <nurse:Nursing x:Name="ccNursing"/>
        </TabItem>
             .......
        </TabItem>
    </TabControl>

Solution

  • It depends on how you initialize your controls, but you could for example handle the Loaded event for a control and perform any re-initialization logcin in there:

    ccNursing.Loading += (s,e) => { /* re-initialize */ };
    

    The other option would be to bind them to a shared DataContext/view model.