Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add WPF page to tabcontrol?

Tags:

c#

wpf

xaml

I have this main wpf window Main WPF window

and this WPF page

WPF page

I need to add this page to tabcontrol in main window

This is my OnRender method

   protected override void OnRender(DrawingContext drawingContext)
    {
        if (ISFirstRender)
        {
            TabItem tabitem = new TabItem();
            tabitem.Header = "Tab 3";
            pan1.Items.Add(tabitem);
            Page1 page1 = new Page1();
            tabitem.Content = new Page1();

            ISFirstRender = false;
        }

        base.OnRender(drawingContext);
    }

after the application running I faced this exception while selecting the new tab Main WPf window after add tab3

Error after select tab 3

I need to know how to add wpf page to existing tabcontroll

like image 434
Moataz Aahmed Mohammed Avatar asked Mar 23 '13 16:03

Moataz Aahmed Mohammed


2 Answers

If you want to add a new Page, as opposed to a UserControl, you can create a new Frame object and place the page in there.

    if (ISFirstRender)
    {
        TabItem tabitem = new TabItem();
        tabitem.Header = "Tab 3";
        Frame tabFrame = new Frame();
        Page1 page1 = new Page1();
        tabFrame.Content = page1;
        tabitem.Content = tabFrame;
        pan1.Items.Add(tabitem);

        ISFirstRender = false;
    }
like image 142
keyboardP Avatar answered Oct 16 '22 18:10

keyboardP


You can add user controls to the TabControl. So go to the add new items and select the user control and make what you want (like what you have in the page). Then add an instance of that user control to the TabControl.

protected override void OnRender(DrawingContext drawingContext)
{
    if (ISFirstRender)
    {
        TabItem tabitem = new TabItem();
        tabitem.Header = "Tab 3";
        pan1.Items.Add(tabitem);

        MyUserControl userControl = new MyUserControl();
        tabitem.Content = userControl;

        ISFirstRender = false;
    }

    base.OnRender(drawingContext);
}
like image 26
Hossein Narimani Rad Avatar answered Oct 16 '22 19:10

Hossein Narimani Rad