[WPF] Using Images in WPF TabControl Headers 탭컨트롤 헤더 이미지 

 

Introduction
In this earlier blog post, I showed some of the useful new features of the WPF TabControl.  In addition to those though, you can also very easily insert images into the Headers of WPF TabControl Tabs for a pleasing and intuitive effect.  Here, I’ll show you how.

The Default WPF Markup
When you create a TabControl instance in WPF, the default layout is quite plain:

TabControl Default UI

 

If you then look at the XAML pane, you’ll see the markup that creates this default appearance:

1
2
3
4
5
6
7
8
        <TabControl HorizontalAlignment="Left" Height="210" Margin="10,34,0,0" VerticalAlignment="Top" Width="272">
            <TabItem Header="TabItem">
                <Grid Background="#FFE5E5E5"/>
            </TabItem>
            <TabItem Header="TabItem">
                <Grid Background="#FFE5E5E5"/>
            </TabItem>
        </TabControl>

The Header property in lines 2 and 5 describe what will appear in the tabs at the top of the control.  I’ll change these in a moment.

The Grid in lines 3 and 6 create the plain Gray area you see that covers the whole of the visible first tab.  You edit this markup so that the tabs contain whatever elements you want in there.  You’ll see this later.

The Header
To replace the default text in the Header with an image, you first need to have an image available to the application.  I usually paste my images into a folder named Images in the solution files:

Image folder

 

With those in place, the next step is to edit the Xaml and replace the default text Header to an Image.  Here’s the Xaml for a TabControl with one Tab:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
  <TabControl Margin="8" Name="TabControl1" TabStripPlacement="Right">
            <TabItem  >
                <TabItem.Header>
                    <Image Width="50" Source="Images/Music2.jpg"/>
                </TabItem.Header>
            </TabItem>

   

   </TabControl>

Note that the TabItem Header has been separated out (by default it’s inline as you saw in the earlier code snippet).  Within the opening and closing tags of the Header I’ve inserted an Image element.  I’ve set the Width of the Image so that it looks right and pointed to one of the image files in the Solution Explorer.  The result is:

Single Header

If this looks a bit unfinished, that’s because it is.  You’d almost certainly want some other elements on the Tab Page itself. 

Next though, I’ll add a couple more tabs and give them Images as Headers.  This markup will do the trick:

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
        <TabControl Margin="8" Name="TabControl1" TabStripPlacement="Right">
            <TabItem  >
                <TabItem.Header>
                    <Image Width="50" Source="Images/Music2.jpg"/>
                </TabItem.Header>
            </TabItem>

            <TabItem >
                <TabItem.Header>
                    <Image Width="50" Source="Images/Video.jpg"/>
                </TabItem.Header>
            </TabItem>

            <TabItem >
                <TabItem.Header>
                    <Image Width="50" Source="Images/books2.jpg"/>
                   </TabItem.Header>
            </TabItem>
        </TabControl>

Now the TabControl looks like this:

Three Tabs

 

Just to make it a bit more realistic, here’s an edited version of the markup that inserts a list of books on the Books tab:

           <TabItem >
                <TabItem.Header>
                    <Image Width="50" Source="Images/books2.jpg"/>
                </TabItem.Header>
                <Grid>
                    <ListBox>
                        <ListBoxItem FontWeight="Bold" FontSize="14" Margin="0,5">Popular Books</ListBoxItem>
                        <ListBoxItem>The Snowman</ListBoxItem>
                        <ListBoxItem>Ground Zero</ListBoxItem>
                        <ListBoxItem>The Gods of Guilt</ListBoxItem>
                        <ListBoxItem>Indigo Slam</ListBoxItem>
                    </ListBox>
                </Grid>
            </TabItem>

The Books Tab now looks like this:

Books Tab With Content

 

Finally, just to clarify – you’re not restricted to either images or text.  This is WPF, so of course you can combine them.  Here’s an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
            <TabItem >
                <TabItem.Header>
                    <StackPanel Orientation="Horizontal" >
                        <Image Width="50" Source="Images/books2.jpg"/>
                        <TextBlock Text="   Books" Foreground="Navy" FontWeight="SemiBold" FontSize="14"/>
                    </StackPanel>
                </TabItem.Header>
                <Grid>
                    <ListBox>
                        <ListBoxItem FontWeight="Bold" FontSize="14" Margin="0,5">Popular Books</ListBoxItem>
                        <ListBoxItem>The Snowman</ListBoxItem>
                        <ListBoxItem>Ground Zero</ListBoxItem>
                        <ListBoxItem>The Gods of Guilt</ListBoxItem>
                        <ListBoxItem>Indigo Slam</ListBoxItem>
                    </ListBox>
                </Grid>
            </TabItem>

Lines 3 to 6 of the code snippet above create a StackPanel and place the Image along with a TextBlock inside it.  The final result is:

Tab Header Text Plus Image

 

Summary
As with so many things in WPF, creating smart looking, intuitive user interfaces is really very easy.

 

[출처] http://vbcity.com/blogs/xtab/archive/2014/09/29/using-images-in-wpf-tabcontrol-headers.aspx

 

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
122 [Windows Programming] 윈도우 스토어 앱 등록 방법 (단계별) - 앱 배포, 앱 출시, 앱 등록 [윈도우 스토어, 윈도 스토어(Windows Store)] file 졸리운_곰 2025.02.15 75
121 [asp.net] [ASP.NET Core] IIS 배포 (게시) file 졸리운_곰 2024.09.20 228
» [WPF] Using Images in WPF TabControl Headers 탭컨트롤 헤더 이미지 file 졸리운_곰 2024.02.18 211
119 [Windows Apps][MFC] HTTP SPY : HTTP 스파이 file 졸리운_곰 2023.11.29 185
118 [Windows Apps][MFC] 2D LUA Based Robot Simulator : 2D LUA 기반 로봇 시뮬레이터 file 졸리운_곰 2023.11.29 183
117 [인공지능 VB.NET] Build Simple AI .NET Library - Part 2 - Machine Learning Introduction : 간단한 AI .NET 라이브러리 구축 - 2부 - 기계 학습 소개 file 졸리운_곰 2023.10.19 151
116 [인공지능 VB.NET] Build Simple AI .NET Library - Part 1 - Basics First : 간단한 AI .NET 라이브러리 구축 - 1부 - 기본 사항 우선 file 졸리운_곰 2023.10.19 267
115 [Windows Programming] [VisualStudio] Nuget 패키지소스 URL file 졸리운_곰 2023.09.14 189
114 [Windows Programming] A brief history of Windows UI platforms : Windows UI 플랫폼의 간략한 역사 file 졸리운_곰 2023.09.04 159
113 [C# Apps] Editor3D: A Windows.Forms Render Control with interactive 3D Editor in C# Editor3D: C#의 대화형 3D 편집기가 포함된 Windows.Forms 렌더 컨트롤 file 졸리운_곰 2023.09.03 543
112 [인공지능 (AI)] Logo Recognition System file 졸리운_곰 2023.06.04 199
111 [C# app] Pythonnet – .NET Core와 Python의 간단한 결합 : Pythonnet – A Simple Union of .NET Core and Python You’ll Love file 졸리운_곰 2023.03.11 183
110 [C# app] Gidon C# 플러그인 프레임워크에 Python 애플리케이션 포함 : Embedding Python Applications within Gidon C# Plugin Framework file 졸리운_곰 2023.03.07 252
109 [C# App] Gidon - Avalonia 기반 MVVM 플러그인 IoC 컨테이너 : Gidon - Avalonia based MVVM Plugin IoC Container file 졸리운_곰 2023.03.07 168
108 [VS2019] [C#] WinForm에 MySQL 연동하기 file 졸리운_곰 2022.12.25 228
107 [윈도우즈 앱 개발]CaptureManager SDK - Capturing, Recording and Streaming Video and Audio from Web-Cams file 졸리운_곰 2021.04.13 264
106 [ASP.NET] JavaScript 및 ASP.NET 개발자를 위한 Blazor 소개 file 졸리운_곰 2021.03.28 297
105 [c# asp.net core] - gRPC 서버, 클라이언트 샘플 튜토리얼 file 졸리운_곰 2021.02.10 400
104 [C#] sqlite on C# 예제로 배우는 C# 프로그래밍 file 졸리운_곰 2021.01.30 401
103 윈도우 wcript.shell 졸리운_곰 2020.09.10 296
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED