I have two grids,
With a TabControl that bind on a ObservableCollection and a ContextMenuItem that will close a Dynamically TabItem.
<Grid> <TabControl Name="mainTabControl" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding ObservableCollectionTabItems}" Background="White" Margin="10,0,0,0"> <TabControl.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Header}" > <TextBlock.ContextMenu> <ContextMenu> <MenuItem Header="Close" Click="MenuItemCloseTab_Click"> </MenuItem> </ContextMenu> </TextBlock.ContextMenu> </TextBlock> </DataTemplate> </TabControl.ItemTemplate> </TabControl> </Grid>
A ListBox that show the name o my Controls.
<Grid Background="#FFD61B1B"> <ListBox x:Name="RightListBox" SelectionMode="Single" SelectionChanged="RightListBox_OnSelectionChanged" IsSynchronizedWithCurrentItem="true" Margin="10,0,0,0"> <ListBox.ItemTemplate> <DataTemplate> <Label Margin="10" Content="{Binding}"/> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid>
To add a new Tab in the Code Behind from MainWindow.xaml:
this._vmMainWindowTabControl.AddTab("ViewOne", "Test");
Who VMMainWindowTabControl.cs is a ViewModel that have: VMBase -> INotifyPropertyChanged Class, VMParentForViews is a empty ViewModel class and VMViewTypeOne another ViewModel who we set the header on a TabItem in the constructor.
public class VMMainWindowTabControl :VMBase
{
private VMParentForViews vmParentForViews;
public VMMainWindowTabControl()
{
ObservableCollectionTabItems = new ObservableCollection<VMParentForViews>();
}
public ObservableCollection<VMParentForViews> ObservableCollectionTabItems { get; set; }
///<summary>
/// I'm trying to get controls to Tabitem with SelectedIndex but I have not success.
/// </summary>
//public int SelectedIndex
//{
// get
// {
// ICollectionView collectionView = CollectionViewSource.GetDefaultView(this.ObservableCollectionTabItems);
// return collectionView.CurrentPosition;
// }
// set
// {
// ICollectionView collectionView = CollectionViewSource.GetDefaultView(this.ObservableCollectionTabItems);
// collectionView.MoveCurrentToPosition(value);
// OnPropertyChanged("SelectedIndex");
// }
//}
/// <summary>
/// Adds the tab to the TabControl
/// </summary>
/// <param name="viewType">Type of the view.</param>
/// <param name="header">Header of the TabItem.</param>
public void AddTab(string viewType, string header)
{
if(viewType.Equals("ViewOne"))
{
vmParentForViews = new VMViewTypeOne(header);
this.ObservableCollectionTabItems.Add(vmParentForViews);
}
// Set the new tab to be the current tab
ICollectionView collectionView1 = CollectionViewSource.GetDefaultView(this.ObservableCollectionTabItems);
if (collectionView1 != null)
{
collectionView1.MoveCurrentTo(vmParentForViews);
}
}
/// <summary>
/// Closes the tab item.
/// </summary>
public void CloseTabItem(Object sender)
{
VMParentForViews vmParentForViews = (sender as MenuItem).DataContext as VMParentForViews;
this.ObservableCollectionTabItems.Remove(vmParentForViews);
}
public void AddElement(Object sender)
{
// How I can do this.
}
}
My problem is that when I click on a ListBoxItem, I get the SelectedItem of this ListBox. But now I doesn't know how I can reference to the corresponding TabItem that I will add this control. This TabItem is saved on the ObservableCollection but I need the sender who clicked on the tab. Maybe the are other ways that I dind't googled well.
Here is a image to explain the treeview on View and ViewModels on my project.
I'm trying with the SelectedIndex property from VMMainWindowTabControl without success to add a element to a TabItem.
VTabItem.xaml is only a canvas item that is shown on each TabItem.
Create new Tab and close this tab a working, many thanks to Nishant Rana with this two tutorials: Creating dynamic TabItem in WPF and Adding a Close Context Menu to TabItem in WPF
Thank you very much for all the help. Greetings and happy new year! :D