Live Chat Icon For mobile
Live Chat Icon

How do I validate the current tab before selection changes to another tab on user click?

Platform: WPF| Category: TabControl

Unfortunately, WPF doesn’t provide a straight forward way of validating a tab page’s content before switching to a new tab, in the first version. One workaround is to listen to the TabItem’s PreviewLostKeyboardFocus event and mark it as Handled as shown below.

[C#]
        public TabValidation()
        {
            InitializeComponent();

            foreach (TabItem item in tabControl1.Items)
            {
                item.PreviewLostKeyboardFocus += new KeyboardFocusChangedEventHandler(item_PreviewLostKeyboardFocus);
            }
        }

        void item_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            // sender is the TabItem.
            if (this.ValidateTab() == false)
                e.Handled = true;
        }

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.