- 전체
- Native Apps
- WinJS App
- C# Apps
- XAML
- VB.net
- VisualC.net
- C++
- MFC
- visual studio mobile app dev
- Azure ms cloud service
- Asp.net
- 인공지능 (AI)
- wpf
- UWP
- MAUI
- asp.net
wpf [WPF] Using Images in WPF TabControl Headers 탭컨트롤 헤더 이미지
2024.02.18 08:07
[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:
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:
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:
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:
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:
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:
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:
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
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
댓글 0
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 수 |
|---|---|---|---|---|
| 8 |
[Windows Apps][MFC] HTTP SPY : HTTP 스파이
| 졸리운_곰 | 2023.11.29 | 185 |
| 7 |
[Windows Apps][MFC] 2D LUA Based Robot Simulator : 2D LUA 기반 로봇 시뮬레이터
| 졸리운_곰 | 2023.11.29 | 183 |
| 6 |
Runtime Object Editor
| 졸리운_곰 | 2017.07.01 | 539 |
| 5 |
** OpenGL 좌표계에서 D3D좌표계로... **
| 가을의곰 | 2017.06.10 | 267 |
| 4 |
SQLite on Visual Studio with NuGet and Easy Instructions
| 졸리운_곰 | 2016.08.11 | 329 |
| 3 | Using SQLite in a Native Visual C++ Application | 졸리운_곰 | 2016.08.11 | 276 |
| 2 |
[학습정리] 민대규님의 학습정리 프로그램 "대굴이의 학습정리"
| 졸리운_곰 | 2015.05.11 | 445 |
| 1 |
[일정관리] 민대규님 제작 "대굴이의 일정관리"
| 졸리운_곰 | 2015.05.11 | 1841 |







