본문 바로가기
프로그래밍 속 지혜/WPF

WPF - TabControl에 UserControl 추가하기

by 생속지 2016. 4. 7.
반응형

Tab Control의 Tab Item을 동적으로 추가하는 방법입니다.

추가된 Tab Item에는 User Control을 표시하도록 하는 예입니다.



1. Window에 TabControl을 추가합니다.


2. TabControl의 Items 속성에 대한 상세정보 화면을 엽니다.

  (Items 속성의 오른쪽 [...] 버튼을 클릭하면 상세정보 화면이 열립니다.)


3. TabItem을 선택 후 [X] 버튼을 클릭하면 TabItem이 삭제됩니다.


4. User Control을 생성합니다.


5. 이번 예제에서는 3개의 User Control을 생성하였습니다.


6. Window의 Load 이벤트를 생성합니다.


7. Window의 Load Event에 TabControl에 TabItem을 추가하는 코드입니다.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AddTabItem
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            // User Control
            UserControl[] userControls = new UserControl[] { new UserControl1(), new UserControl2(), new UserControl3()};

            // Add TabItem
            foreach (UserControl uc in userControls)
            {
                TabItem tabItem = new TabItem();
                tabItem.Header = "New Tab";
                tabItem.Content = uc;

                tabControl1.Items.Add(tabItem);
            }
        }
    }
}


8. 아래의 이미지는 User Control이 추가된 Tab Control입니다.




반응형

댓글