6 Replies - 1052 Views - Last Post: 05 February 2013 - 10:45 AM Rate Topic: -----

#1 erburrell  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 146
  • Joined: 22-December 09

How to add event handler to the tab of a TabItem?

Posted 05 February 2013 - 08:23 AM

I am trying to add an event to allow someone to change the header on a tab item. When someone hits New from the file menu, I want the program to add a new tab with a default header. They can then click the header and change the name. I have a function that calls a dialog box that allows them to enter then new name. My question is, how do I assign the new function to the mouse click event on the tab of the TabItem?

Thanks,

Ed

The code that I have is as follows:
    public partial class MainWindow : Window {
        public MainWindow() {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e) {
            leftDock.Width = 0;
            mainDock.Margin = new Thickness(0, 23, 0, 23);
            if ((mainTabControl.FindName("New Doc") == null)) {
                AddNewTab("New Doc", mainTabControl);
            }
            


        }

        private void AddNewTab(string tabName, TabControl aTabControl) {
            TabItem newTab = new TabItem();
            newTab.Header = tabName;
            aTabControl.Items.Add(newTab);
            aTabControl.SelectedItem = (TabItem)aTabControl.FindName(tabName);
            
            return;
        }

        private void TabItemChangeName(object sender, RoutedEventArgs e) {
            Views.InputWindow nameWindow = new Views.InputWindow();
            namewindow.Owner = this;
            namewindow.ShowDialog();

            if (namewindow.DialogResult == true) {
                ((TabItem)sender).Header = namewindow.inputTextBox.Text;
            }
        }
    }


Is This A Good Question/Topic? 0
  • +

Replies To: How to add event handler to the tab of a TabItem?

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,480
  • Joined: 05-May 12

Re: How to add event handler to the tab of a TabItem?

Posted 05 February 2013 - 09:06 AM

What have you tried so far? Does the click event for the tab or tab control not do what you want?
Was This Post Helpful? 0
  • +
  • -

#3 erburrell  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 146
  • Joined: 22-December 09

Re: How to add event handler to the tab of a TabItem?

Posted 05 February 2013 - 09:44 AM

To be honest, I am asking how to add the click event to the tab portion of the TabItem. How do I single out only that portion and assign it there?

Thanks
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,480
  • Joined: 05-May 12

Re: How to add event handler to the tab of a TabItem?

Posted 05 February 2013 - 09:52 AM

Does the Click event on a TabPage not work?
http://msdn.microsof...age_events.aspx

At worse, you could do some hittesting on the click event and determine if the mouse is positioned within the tab portion or not.
Was This Post Helpful? 0
  • +
  • -

#5 erburrell  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 146
  • Joined: 22-December 09

Re: How to add event handler to the tab of a TabItem?

Posted 05 February 2013 - 10:06 AM

I have changed the code to add the event handler to the tabitem, but when I click it does nothing. here is my code:

private void AddNewTab(string tabName, TabControl aTabControl) {
            TabItem newTab = new TabItem();
            newTab.Header = tabName;
            aTabControl.Items.Add(newTab);
            aTabControl.SelectedItem = (TabItem)aTabControl.FindName(tabName);
            ((TabItem)aTabControl.SelectedItem).MouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(MainWindow_MouseLeftButtonDown);
            return;
        }

        void MainWindow_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e) {
            Views.InputWindow nameWindow = new Views.InputWindow();
            namewindow.Owner = this;
            namewindow.ShowDialog();

            if (namewindow.DialogResult == true) {
                ((TabItem)sender).Header = namewindow.inputTextBox.Text;
            }
        }

Was This Post Helpful? 0
  • +
  • -

#6 erburrell  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 146
  • Joined: 22-December 09

Re: How to add event handler to the tab of a TabItem?

Posted 05 February 2013 - 10:44 AM

I have it working now. My code is below. Apparently, it did not like the single click event being assigned, which makes sense when you think about the single click event is already assigned to the selection of a tab. To change it (which would have been crazy!) I think I would have needed to override the event handler.

Thanks,

Ed

private void AddNewTab(string tabName) {
            TabItem newTab = new TabItem();
            newTab.Header = tabName;
            newTab.Name = tabName.Replace(" ", string.Empty);
            newTab.MouseDoubleClick += new System.Windows.Input.MouseButtonEventHandler(newTab_MouseDoubleClick);
            mainTabControl.Items.Add(newTab);
            mainTabControl.SelectedItem = (TabItem)mainTabControl.FindName(newTab.Name);            
        }

        void newTab_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e) {
            Views.InputWindow nameWindow = new Views.InputWindow();
            namewindow.Owner = this;
            //namewindow.ShowDialog();

            if (namewindow.ShowDialog() == true) {
                ((TabItem)sender).Header = namewindow.inputTextBox.Text;
            }
        }

        void FileNewEventHandler(object sender, RoutedEventArgs e) {
            AddNewTab("New Doc " + (mainTabControl.Items.Count + 1).ToString());
        }

Was This Post Helpful? 1
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,480
  • Joined: 05-May 12

Re: How to add event handler to the tab of a TabItem?

Posted 05 February 2013 - 10:45 AM

The MouseLeftDown event is not listed as a supported event for the tabpage based on that link I posted earlier. Have you tried the Click event?

Looks like you figured out something.

This post has been edited by Skydiver: 05 February 2013 - 10:46 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1