79417916

Date: 2025-02-06 12:37:06
Score: 0.5
Natty:
Report link

so just to keep you all updated. I have been working on this issue this past week and it seems to be fixed, or better said .. hacked to work as intended.

I have not found what exactly is causing this issue, but with the help of third-party tool "Spoon" and Debug.WriteLine(Enviroment.StackTrace) I have been able to compare normal and abnormal behaviour of tabSelectionChanged. Every time the tab misfired it was by part caused by MouseCaptureLost on some part of our UI. Image from WinMerge compare between StackTrace of normal and abnormal tab behaviour.

To prevent this from happening I have built custom checking logic and sanitized the input of event handlers for our custom class ClosableTab.cs,

    // Internal method to check for tab change validity
    private bool EvaluateTabFocus()
    {
        _mouseOverTabFocus = IsMouseOver;
        bool ret = _newTabFocus || _mouseOverTabFocus || _codeNavigationFocus;
        _codeNavigationFocus = _newTabFocus = false;
        return ret;
        } 

    // Override OnSelected - Show the Close Button
    protected override void OnSelected(RoutedEventArgs e)
    {
        // Check if the event was generated by the same type
        if (e.OriginalSource.GetType() != this.GetType())
            return;
    
        // Check if the event is happening whilst the mouse is over top of the header or there is automatic navigation to this tab through code.
        if (EvaluateTabFocus()) 
        {
            _selectionIsValid = true;
            base.OnSelected(e);
            ((CloseableHeader)this.Header).button_close.Visibility = Visibility.Visible;
        }
        else
        {
            _selectionIsValid = false;
            base.OnSelected(e);
        }
    }

I then use this _selectionIsValid inside MainWindow.xaml.cs private void tcMain_SelectionChanged(object sender, SelectionChangedEventArgs e) to check for valid tab changes and repeatadly discard the bad ones.

It seems to be working now so we will be monitoring the behaviour. If the issue appears again I may return but for now this cutom logic is doing fine. Thanks all to your helpfull insights.

Reasons:
  • Blacklisted phrase (0.5): Thanks
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Marek Sýkorka