tags:

views:

54

answers:

4

how can i, in C#/WPF implement an application where i can open/close a new tab? i am thinking i will have to create a "template" user control and programmatically, create a new instance of the control (tab item) and add it into the tab control?

i am new to C#/WPF so how can i get started with this?

another thing is how can i modify or access child controls when i dont have an ID.

+2  A: 

The example in this link does more or less exactly what you want: http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

It's also a great intro to MVVM.

cristobalito
i am also trying to get started with MVVM, i saw that article before, but found it hard to get started, maybe i am new to C#/WPF. but any other MVVM links to share? maybe video tutorials? i learn better with videos sometimes.
jiewmeng
I don't have any video links handy (suggest searching channel 9) but just recommend focusing on one concept at a time. Initially, WPF would seem to give the easiest wins, and once you've got your head around that, try MVVM again.
cristobalito
A: 

This is the code that I used.

    private void addtabbutton_Click(object sender, RoutedEventArgs e)
    {
        // We use tabItem1 and codebox as template<typename T> for the new objects.
        var tabitem = new System.Windows.Controls.TabItem();
        tabitem.ContextMenu = tabItem1.ContextMenu;
        tabitem.ContextMenuClosing += tabItem1_ContextMenuClosing;
        tabitem.ContextMenuOpening += tabItem1_ContextMenuOpening;
        tabitem.Header = "Code" + NewTabItemIndex.ToString();
        tabitem.Height = tabItem1.Height;
        tabitem.Width = tabItem1.Width;
        tabitem.HorizontalAlignment = tabItem1.HorizontalAlignment;
        tabitem.VerticalAlignment = tabItem1.VerticalAlignment;
        tabitem.DataContext = tabItem1.DataContext;
        var textbox = new System.Windows.Controls.TextBox();
        tabitem.Content = textbox;
        textbox.DataContext = codebox.DataContext;
        textbox.LayoutTransform = codebox.LayoutTransform;
        textbox.AcceptsReturn = true;
        textbox.AcceptsTab = true;
        textbox.Height = this.codebox.Height;
        textbox.HorizontalAlignment = codebox.HorizontalAlignment;
        textbox.VerticalAlignment = codebox.VerticalAlignment;
        NewTabItemIndex++;
        this.tabControl1.Items.Add(tabitem);
    }

You can see that I started off with one tab item, tabItem1, in the box. Then I essentially copy it's characteristics into a new TabItem. Then I add that TabItem into my TabControl.

DeadMG
+1  A: 

Hi

You can do this very eaisly with ObservableCollections.

xaml

    <TabControl ItemsSource="{Binding EmpList }">
        <TabControl.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding FirstName }"></TextBlock>
            </DataTemplate>
        </TabControl.ItemTemplate>
    </TabControl>

Code Asumeing you are using MVVM

Create a EmpList Observablecollection in your ViewModel

so when you add a new object in Observablecollection , tab control listen for the change and add new tab for you.

saurabh