Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Guys,

I have decided to try out WPF and I am stuck on naming the tabcontrol header. Using Josh's post my wish is to a have Tab created everytime a user clicks a menu and the tab's header will be set to the calling menuitem header. How can I accomplish this in the simplest way. Please advice on which feature I should use. Thanx in advance!

Stan
Posted
Comments
Suvendu Shekhar Giri 15-Jul-15 4:36am    
Have you tried anything? Please share the relevant code you have tried.
Sausostan 15-Jul-15 12:21pm    
Thank you for replying Giri...here is what I have done
Sausostan 15-Jul-15 12:49pm    
I had to paste what I have done as a solution below. Didnt know whatelse to do!

1 solution

menu xaml

XML
<Menu Name="mnuMain" DockPanel.Dock="Top" HorizontalAlignment="Stretch"  Width=" Auto" MenuItem.Click ="menuClick">
              <MenuItem Header="_File">
                  <MenuItem Command="{Binding NewWorkspaceCommand}" Header="_New Tab" InputGestureText="Ctrl + N" />
                  <MenuItem Command="{Binding CloseWorkspaceCommand}" Header="_Close Tab" InputGestureText="Ctrl + F4" />
                  <MenuItem Command="{Binding ExitCommand}" Header="E_xit" InputGestureText="Ctrl + X" />
              </MenuItem>


tab control is as follows

XML
<TabControl.Background >
                <ImageBrush ImageSource="/Resources/images/background.png" Stretch="UniformToFill" />
            </TabControl.Background>

            <TabControl.ItemTemplate>
                <DataTemplate>
                    <WrapPanel>
                        <TextBlock Text="{Binding Header}" />
                        <Button Command="{Binding CloseCommand}" Content="X" Margin="4,0,0,0" FontFamily="Courier New" Width="17" Height="17" VerticalContentAlignment="Center" />
                    </WrapPanel>
                </DataTemplate>

            </TabControl.ItemTemplate>

        </TabControl>



viewmodel

C#
 public class MainViewModel : ViewModelBase
    { 
             
        #region Constructor

        public MainViewModel()
        {
            
            Workspaces = new ObservableCollection<workspaceviewmodel>();
            Workspaces.CollectionChanged += Workspaces_CollectionChanged;
             
        }

        #endregion
          
        #region Event Handlers
        public void  menuClick(object sender, RoutedEventArgs e)
        {
           MenuItem item = e.OriginalSource as MenuItem;
          if (null != item)
            {
                CallingMenuName = item.Name;{}
                
            }
         }
        
        void Workspaces_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null && e.NewItems.Count != 0)
                foreach (WorkspaceViewModel workspace in e.NewItems)
                    workspace.RequestClose += this.OnWorkspaceRequestClose;

            if (e.OldItems != null && e.OldItems.Count != 0)
                foreach (WorkspaceViewModel workspace in e.OldItems)
                    workspace.RequestClose -= this.OnWorkspaceRequestClose;
        }

        private void OnWorkspaceRequestClose(object sender, EventArgs e)
        {
            CloseWorkspace();
        }

        #endregion

        #region Commands

        private DelegateCommand _exitCommand;
        public ICommand ExitCommand
        {
            get { return _exitCommand ?? (_exitCommand = new DelegateCommand(() => Application.Current.Shutdown())); }
        }

        private DelegateCommand _newWorkspaceCommand;
        public ICommand NewWorkspaceCommand
        {
            get { return _newWorkspaceCommand ?? (_newWorkspaceCommand = new DelegateCommand(NewWorkspace)); }
        }

        private void NewWorkspace()
        {
            frmCustomer oCustomer = new frmCustomer();
          
            var workspace = new WorkspaceViewModel { Header = "New Tab" } ;
            
            Workspaces.Add(workspace); 
            /// = oCustomer.Content; 
            SelectedIndex = Workspaces.IndexOf(workspace);
           
        }

        private DelegateCommand _closeWorkspaceCommand;
        public ICommand CloseWorkspaceCommand
        {
            get { return _closeWorkspaceCommand ?? (_closeWorkspaceCommand = new DelegateCommand(CloseWorkspace, () => Workspaces.Count > 0)); }
        }

        private void CloseWorkspace()
        {
            Workspaces.RemoveAt(SelectedIndex);
            SelectedIndex = 0;
        }

     
        
        #endregion

        #region Public Members

        public ObservableCollection<workspaceviewmodel> Workspaces { get; set; }



        private int _selectedIndex = 0;
        public int SelectedIndex
        {
            get { return _selectedIndex; }
            set
            {
                _selectedIndex = value;
                OnPropertyChanged("SelectedIndex");
            }
        }

        private string _CallingMenuName = "";
        public string CallingMenuName
        {
            get { return _CallingMenuName; }
            set
            {
                _CallingMenuName = value;
                OnPropertyChanged("CallingMenuName");
            }
        }

        #endregion
    }
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900