Articles in this section
Category / Section

How to make events fire with unloading of the WPF TabControlExt?

1 min read

Events of the elements in the TabItemExt’s content does not fire after TabItem selection change when hooked in constructor. This is due to content unloading behaviour of the WinForms TabControlExt. Content of TabItemExt is unloaded on selection change and loaded again when this tab item is selected. This problem can be resolved by hooking all the events in the Loaded event instead of the constructor.

For example, there are two TabItems and their contents are UserControl1 and UserControl2, respectively.

In the UserControl1, events of its elements are hooked in its constructor.

In the UserControl2, events of its elements are hooked in its Loaded event.

MainWindow.xaml

<sync:TabControlExt>
      <sync:TabItemExt Header="TabItem 1">
            <local:UserControl1/>
      </sync:TabItemExt>
      <sync:TabItemExt Header="TabItem 2">
            <local:UserControl2/>
      </sync:TabItemExt></sync:TabControlExt>

UserControl1.xaml and UserControl2.xaml

<Grid>
     <StackPanel>
          <TextBox Height="20" Width="200" x:Name="TextBox"/>
          <TextBlock Foreground="Red" HorizontalAlignment="Center" FontStyle="Italic" x:Name="TextBlock"/>
     </StackPanel>
</Grid>

UserControl1.xaml.cs

public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
            TextBox.TextChanged += TextBox_TextChanged;
            Unloaded += UserControl1_Unloaded;
        }
        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextBlock.Text = TextBox.Text;
        }
        private void UserControl1_Unloaded(object sender, RoutedEventArgs e)
        {
            TextBox.TextChanged -= TextBox_TextChanged;
            Unloaded -= UserControl1_Unloaded;
        }
    }

UserControl2.xaml.cs

public partial class UserControl2 : UserControl
    {
        public UserControl2()
        {
            InitializeComponent();
            Loaded += UserControl1_Loaded;
        }
        void UserControl1_Loaded(object sender, RoutedEventArgs e)
        {
            TextBox.TextChanged += TextBox_TextChanged;
            Unloaded += UserControl1_Unloaded;
        }
        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextBlock.Text = TextBox.Text;
        }
        private void UserControl1_Unloaded(object sender, RoutedEventArgs e)
        {
            TextBox.TextChanged -= TextBox_TextChanged;
            Unloaded -= UserControl1_Unloaded;
        }
    }

The following screenshots show the TextChanged event failed to fire after Tab selection change in TabItem 1:

Figure 1: TextChanged event firing before tab item selection change

 

TextChanged event not firing after tab item in WPF TabControl

Figure 2: TextChanged event not firing after tab item selection change

Conclusion

I hope you enjoyed learning about how to make events fire with the unloading of the TabControlExt.

You can refer to our WPF TabControl feature tour page to know about its other groundbreaking feature representations and documentation. You can also explore our WPF TabControl Example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!


Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied