In my wpf program i would like to have tabs that would be generated from array or list. I would like to edit files with each tab. Each tab would have corresponding folder with same name, so all tabs should look same (this is why I used DataTemplates), since files in all directories are all generated with same names but their content is different. I have code that generates tabs from array and adds names to tabs.
public class MainWindowViewModel
{
public ObservableCollection<TabViewModel> Tabs { get; set; }
public MainWindowViewModel()
{
this.Tabs = new ObservableCollection<TabViewModel>();
var location = System.AppDomain.CurrentDomain.BaseDirectory.ToString();
string[] folderList = new string[] { "Folder1", "Folder2" };
foreach (string folder in folderList)
{
this.Tabs.Add(new TabViewModel(folder));
string newLocation = location + folder + "\\";//i would like to point tab to this directory
}
}
}
public class TabViewModel
{
public string Name { get; set; }
public TabViewModel(string name)
{
this.Name = name;
}
}
<TabControl ItemsSource="{Binding Tabs}">
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate >
<TextBox x:Name="fileTextBox"/>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
But I have 2 problems:
- I put textbox in content of a tab and if I create more than 1 tab they all share same content in textbox. I would need to make separate instance for each tab.
- When tab is created I would need to assign directory(which i stored in newLocation string) to it and then display file (e.g. sample.txt) in textbox (fileTextBox).