In C# WPF, why is my TabControl's SelectionChanged event firing too often?

前端 未结 4 1502
小鲜肉
小鲜肉 2020-11-30 02:41

I have a tabbed GUI with each tab containing a Frame. In one of these Frames there is a DataGrid. When the user selects this tab, I need my datagrid sorted, so I\'m using th

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-30 03:42

    The TabControl.SelectionChanged is the same event as a ComboBox.SelectionChanged

    It originates from Selector.SelectionChanged.

    So, if you do not mark your event as handled in your event handler, it will bubble up the tree, and eventually arrive at your TabControl, which is causing this "firing too often" issue.

    Mark your event as handled in your SelectionChanged of your ComboBox/ListBox/ListView/any other Selector you use in your DataGrid like so:

    private void MyComboBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        e.Handled = true;
    }
    

    And this inconvenience will go away ;).

提交回复
热议问题