Hi i have a tab load this usercontrol. when i wish to close this tab, i wish to call this cancelbutton_click event to pop up confirmation on closing, if OK, then close, if Cancel, the tab stays.
if i use Unloaded event, it will pop up twice before closing.
 private void UserControl_Unloaded(object sender, RoutedEventArgs e)
        {
              cancelbutton_click(sender,null);
        }
Cancel button:
private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
  MessageBoxResult objResult = MessageBox.Show("\nAre you sure you want to cancel?", "Cancel Confirmation", MessageBoxButton.OKCancel);
            if (objResult == MessageBoxResult.OK)
            {
                try
                {
                    TabItem tabItem = parentWindow.FindTabItemByName(ControlType.BusinessesContractors.ToString(), false);
                    this.parentWindow.mainTabControl.Items.Remove(tabItem);
                    this.parentWindow.statusTextBlock.Text = "Ready";
                }
       开发者_开发技巧         catch (Exception ex)
                {
                }
            }
how to resolve this? thanks
Unloaded is called when the control is already being removed, it's not an event you want to handle often, just create a button which is supposed to close the tab, handle it's click, check if the user wants to cancel via the dialogue and close the tab if he does not.
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论