有没有办法取消 TabControl.Items.CurrentChanging?

发布于 2024-10-02 16:47:47 字数 362 浏览 5 评论 0原文

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

我尝试处理 TabControl.Items.CurrentChangingItems 属性是和 ItemCollection)事件设置 e.Cancel (CurrentChangingEventArgs) 为 true,但 UI 会使用新选项卡进行更新,尽管集合中的项目未发生更改。

有什么方法可以防止用户在不满足条件时切换到不同的 TabItem 吗?

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

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

べ繥欢鉨o。 2024-10-09 16:47:47

我不知道发生这种情况的确切原因,这让我非常恼火。

但这是我的解决方法:

在下面的示例中,复选框“锁定”当前选项卡。所以选中意味着用户无法更改选项卡。

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

        e.Cancel = true;

        tabControl1.SelectedItem = item;
    }
}

基本上,发生的情况是(如果我理解正确的话)视觉树得到更新,但逻辑树没有更新。上述方式强制视觉与逻辑树同步。

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.

╰つ倒转 2024-10-09 16:47:47

您还可以处理每个上的 PreviewLostKeyboardFocus 事件TabItem,并设置事件参数的 Handled 属性为 true 以防止切换到另一个选项卡:

protected void tabItem_PreviewLostKeyboardFocus(object sender,
    KeyboardFocusChangedEventArgs e)
{
    if (!ValidateTabItem((TabItem) sender)) {
        e.Handled = true;
    }
}

请参阅 http://www.netframeworkdev.com/windows-presentation-foundation-wpf/how-to-cancel-navigation- Between-tabitems-in-a-tabcontrol-84994.shtml

You can also handle the PreviewLostKeyboardFocus event on each TabItem, and set the Handled property of the event arguments to true to prevent switching to another tab:

protected void tabItem_PreviewLostKeyboardFocus(object sender,
    KeyboardFocusChangedEventArgs e)
{
    if (!ValidateTabItem((TabItem) sender)) {
        e.Handled = true;
    }
}

See http://www.netframeworkdev.com/windows-presentation-foundation-wpf/how-to-cancel-navigation-between-tabitems-in-a-tabcontrol-84994.shtml.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文