1

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.

FPGA
  • 3,525
  • 10
  • 44
  • 73
  • 2
    Don't create or manipulate UI elements in procedural code. – Federico Berasategui Sep 24 '13 at 19:23
  • @HighCore why not if i give each one a name and know how to get them? – FPGA Sep 24 '13 at 19:25
  • 1
    Because that's a crappy procedural approach. Learn MVVM. [Here](http://stackoverflow.com/a/15210593/643085) is an example of how you deal with a `TabControl` in WPF. – Federico Berasategui Sep 24 '13 at 19:27
  • 2
    Hi @user1492051, Highcore is right MVVM is a much better approach for this kind of thing. You'll want to bind your TabControl's Items property to your list of objects, which will automatically cause WPF to draw each item as a `TabItem`. You can then use the `` to bind the `TabItem` properties such as Name or Header to the associated item on the data object. If you're interested, I have a [simple MVVM example](http://rachel53461.wordpress.com/2011/05/08/simplemvvmexample/) on my blog that you might find useful as well if you're just starting with MVVM. – Rachel Sep 24 '13 at 20:28
  • @Rachel thank you, thats what i was looking for, it just didn't feel right doing it my way – FPGA Sep 24 '13 at 20:33
  • @Rachel thanks. I keep linking your "Transition from winforms to WPF" answers to everyone. =) – Federico Berasategui Sep 24 '13 at 20:34
  • @HighCore I was wondering why [that post](http://stackoverflow.com/a/15684569/302677) kept getting so many views, thanks! :) – Rachel Sep 24 '13 at 20:39

0 Answers0