简体   繁体   English

WPF-取消选择TabControl中的选项卡会导致问题

[英]WPF - Cancel selection of a tab in TabControl causes problems

I have an application with some TabControl, and in a specific tab a long computation can be started. 我有一个带有某些TabControl的应用程序,并且在特定的选项卡中可以启动长时间的计算。 I want the user to confirm leaving the tab and aborting the computation. 我希望用户确认离开选项卡并中止计算。 So far, I created a Behavior and attached it to the tabcontrol. 到目前为止,我创建了一个Behavior并将其附加到tabcontrol。 (code at the end). (代码末尾)。 My problem: suppose I want to conifrm leaving tab #3. 我的问题:假设我要确认离开标签#3。 I choose tab #2 -> confirmation dialog pops and I choose no (CanNavigateFromMe() == false), and I return to tab #3. 我选择选项卡#2->弹出确认对话框,然后选择否(CanNavigateFromMe()== false),然后返回选项卡#3。 Again, I choose tab #2 and get same behavior. 同样,我选择选项卡#2并得到相同的行为。 I want to choose it for the 3rd time - now, clicking the header does not fire the CurrentChanging event! 我想第三次选择它-现在,单击标题不会触发CurrentChanging事件!

code for the behavior: 行为代码:

    protected override void OnAttached()
    {
        base.OnAttached();
        AssociatedObject.Loaded += new System.Windows.RoutedEventHandler(AssociatedObject_Loaded);
    }

    void AssociatedObject_Loaded(object sender, System.Windows.RoutedEventArgs e)
    {
        // required in order to get CurrentItemChanging
        AssociatedObject.IsSynchronizedWithCurrentItem = true;

        AssociatedObject.Items.CurrentChanging += new CurrentChangingEventHandler(Items_CurrentChanging);
    }



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

        var view = item as FrameworkElement;
        if (view == null)
        {
            return;
        }
        IAllowNavigation allowNavigation = view.DataContext as IAllowNavigation;
        if ((allowNavigation != null) &&
            (allowNavigation.CanNavigateFromMe() == false))
        {
            e.Cancel = true;
            AssociatedObject.SelectedItem = view;
        }
    }

After some help from friends, I found out I needed to: 1. call Refresh() on the collection if cancelling the selection. 在朋友的帮助下,我发现我需要:1.如果取消选择,请在集合上调用Refresh()。 2. ensure the original selection is made if I decide to allow the selection (this involves user input and takes time, meanwhile ecents inside other tabs can change the selected item) 2.如果我决定允许选择,请确保进行了原始选择(这涉及到用户输入并需要时间,同时其他选项卡中的分隔符可以更改所选项目)

    void Items_CurrentChanging(object sender, CurrentChangingEventArgs e)
    {
        var newItem = AssociatedObject.SelectedItem;
        var item = ((ICollectionView)sender).CurrentItem;


        var view = item as FrameworkElement;
        if (view == null)
        {
            return;
        }
        IAllowNavigation allowNavigation = view.DataContext as IAllowNavigation;
        if ((allowNavigation != null) &&
            (allowNavigation.CanNavigateFromMe() == false))
        {
            e.Cancel = true;
            AssociatedObject.SelectedItem = view;

        }
        else
        {
            AssociatedObject.SelectedItem = newItem;
        }

        ((ICollectionView)sender).Refresh();

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM