有没有办法取消TabControl.Items.CurrentChanging? [英] Is there a way to cancel TabControl.Items.CurrentChanging?

查看:85
本文介绍了有没有办法取消TabControl.Items.CurrentChanging?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不幸的是,没有TabControl.SelectionChanging事件(Selector.SelectionChanging),我正在努力实现此行为,所以我可以取消更改请求.

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

我试图处理TabControl.Items.CurrentChanging(Items属性是ItemCollection)事件,将(CurrentChangingEventArgs的)e.Cancel设置为true,但是UI会用新选项卡更新,尽管项目在收藏夹中未更改.

I tried to handle the TabControl.Items.CurrentChanging (the Items property is and ItemCollection) event setting e.Cancel (of the CurrentChangingEventArgs) to true, but the UI is is updated with the new tab although the item is not changed in the collection.

有什么方法可以防止用户在条件不满意时切换到其他TabItem吗?

Is there any way to prevent user from switching to a different TabItem when a condition is dissatisfied?

推荐答案

我不知道发生这种情况的确切原因,这极大地困扰了我.

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

但这是我的解决方法:

在下面的示例中,复选框锁定"了当前选项卡.因此,选中表示用户无法更改标签.

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.

这篇关于有没有办法取消TabControl.Items.CurrentChanging?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆