Is there a way to cancel TabControl.Items.CurrentChanging?

前端 未结 2 426
傲寒
傲寒 2021-01-18 16:32

There is unfortunately no TabControl.SelectionChanging event (Selector.SelectionChanging), I am struggling to implement this behavior so I can cancel the changing request.

2条回答
  •  太阳男子
    2021-01-18 16:41

    I don't know the exact reason why this happens, and it annoys me greatly.

    But here's my workaround for it:

    In the sample below, checkbox is "locking" the current tab. So checked means user can't change tab.

    void Items_CurrentChanging(object sender, CurrentChangingEventArgs e)
    {
        if (checkBox1.IsChecked.Value)
        {
            var item = ((ICollectionView)sender).CurrentItem;
    
            e.Cancel = true;
    
            tabControl1.SelectedItem = item;
        }
    }
    

    Basically, what happens is (if I understand this correctly) the visual tree gets updated, but the logical tree does not. The above way forces the visual to sync with the logical tree.

提交回复
热议问题