Refactor Code

  • Gravatar
    SelectionChanged Event of Tab Control Firing on Form Load

    by azamsharp on 11/4/2008 12:38:04 PM
  • I am developing a WPF application and for some reason whenever the form is loaded the SelectionChanged event of the TabControl gets fired. How can I stop this from happening?
  • // attaching the event in the Form constructor
    documentTabs.SelectionChanged += new SelectionChangedEventHandler(documentTabs_SelectionChanged);
    documentTabs.SelectedIndex = -1;

    void documentTabs_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
    // get the selected item
    TabItem item = documentTabs.SelectedItem as TabItem;

    if (item == null) return;

    MessageBox.Show(item.Header.ToString());
    }
  • Refactor it!
  • Gravatar
    Got the solution! Just need to attach it in the Form_Loaded event.
    by azamsharp on 11/4/2008 12:47:42 PM
  • this.Loaded += new RoutedEventHandler(AttributeControl_Loaded);

    void AttributeControl_Loaded(object sender, RoutedEventArgs e)
    {
    documentTabs.SelectionChanged += new SelectionChangedEventHandler(documentTabs_SelectionChanged);

    documentTabs.SelectedIndex = -1;
    }
Please log in to refactor the code! Login