menu xaml
<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
<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
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);
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
}
}