I have a TextBox
, a TabControl
and a simple object called someobject
.
I create a List<someobject>
object and fill it with a dynamic number of someobject
.
Foreach someobject
in that list I create a TabItem
give its name the same value as the name property in that someobject
.
Here is a commented code of what I am doing
public partial class MainWindow : Window
{
List<SomeObject> list;
TextBox textbox = new TextBox() { Name = "textbox1" };
public MainWindow()
{
InitializeComponent();
//create a test list of someobject
list = new List<SomeObject>()
{
new SomeObject() {name = "Italian", description = "Italian description"},
new SomeObject() {name = "German", description = "german description"},
new SomeObject() {name = "French", description = "french description"}
};
//add a tab item for each object
foreach(SomeObject someobj in list)
{
tabControl1.Items.Add(new TabItem { Name = someobj.name,Header = someobj.name });
}
}
//on selected tabitem changed event set the content of all tab items to null and set the content of the selected tabitem to the TextBox textbox1
private void tabControl1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
foreach(TabItem tab in (sender as TabControl).Items)
{
tab.Content = null;
}
string someobjectnameproperty = ((sender as TabControl).SelectedItem as TabItem).Name;
textbox.Text = list.Find(obj => obj.name == someobjectnameproperty).description;
((sender as TabControl).SelectedItem as TabItem).Content = textbox;
}
//someobject class
class SomeObject
{
public string name { get; set; }
public string description { get; set; }
}
}
I did all of the above because I don't want to have a textbox control inside each tab item, the code is working perfectly but is there a more convienient way to achieve the same result with wpf?
this is just an example for demonstration.