2

I am doing something like the following to prevent a tab selection from changing:

 tabControl.Items.CurrentChanging += new CurrentChangingEventHandler(Items_CurrentChanging);

 void Items_CurrentChanging(object sender, CurrentChangingEventArgs e)
 {
     if( dataIsInvalid )
     {
          // Show some popup message
          var item = ((ICollectionView)sender).CurrentItem;
          e.Cancel = true;
          tabControl.SelectedItem = item; // !! This causes the CurrentChanging event to happen twice !! 
                                          // But without this the visual tree does not update! :( 
     }
}

However the problem that I am seeing right now is that the last line above causes a CurrentChanging event to happen a second time, and my popup message is displayed twice. Not only that, it is displayed the second time after bringing another window into focus first, and then putting focus back on my tab control's window. Any ideas why this could be happening?

--EDIT-- It looks like I should really only need e.Cancel and not require updating the SelectedItem again. However the visual tree is not updated unless I do this. Is there any way I can ensure the visual tree of the tab control is updated after e.Cancel occurs, without having to update the SelectedItem?

4

2 回答 2

2

是的,这有点奇怪......您可以确定地取消挂钩并将当前更改的事件处理程序挂钩PreviewMouseDown在选项卡控件中。

并且在 CurrentChanging 本身中解开处理程序,这样它就不会被调用多次。

    private void MyTabControl_PreviewMouseDown(object sender, MouseButtonEventArgs e)
    {
        var tabControl = sender as TabControl;

        if (tabControl != null)
        {
            var temp = new CurrentChangingEventHandler((sender1, e1) => { });
            var handler = new CurrentChangingEventHandler(
                (sender1, e1) =>
                    {
                        var item = ((ICollectionView) sender1).CurrentItem;
                        if (item != null && dataIsInvalid)
                        {
                            e1.Cancel = true;
                            Dispatcher.BeginInvoke(
                                new Action(() =>
                                   {
                                       tabControl.SelectedItem = item;
                                   }));
                        }

                        tabControl.Items.CurrentChanging -= temp;
                    });

            temp = handler;
            tabControl.Items.CurrentChanging -= handler;
            tabControl.Items.CurrentChanging += handler;
        }
    }

我希望这有帮助。

于 2011-10-04T09:47:45.577 回答
0

这个问题很老,但我现在用选项卡控件遇到了这个问题。我所做的只是在调用它之前解开处理程序。

InsturmentTabs.Items.CurrentChanging -= new CurrentChangingEventHandler(Items_CurrentChanging);
InsturmentTabs.Items.CurrentChanging += new CurrentChangingEventHandler(Items_CurrentChanging);

private void Items_CurrentChanging(object sender, CurrentChangingEventArgs e)
    {
        if (condition)
        {
            var item = ((ICollectionView)sender).CurrentItem;
            e.Cancel = true;

            InsturmentTabs.SelectedItem = item;              
        }                                          
    }      
于 2015-06-11T20:38:11.727 回答