- 전체
- 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
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 수 |
|---|---|---|---|---|
| 15 | C# getMethod : Call Method by Name | 졸리운_곰 | 2017.07.01 | 258 |
| 14 | Reflection for C++ | 졸리운_곰 | 2017.07.01 | 343 |
| 13 |
Load an EXE File and Run It from Memory
| 졸리운_곰 | 2017.07.01 | 333 |
| 12 |
.Net code injection 닷넷 코드 인젝션
| 졸리운_곰 | 2017.07.01 | 298 |
| 11 |
Three Ways to Inject Your Code into Another Process
| 졸리운_곰 | 2017.07.01 | 547 |
| 10 |
Visual C++를 사용하여 플랫폼 간 앱 제작
| 졸리운_곰 | 2015.08.16 | 645 |
| 9 |
webdav c++ lib 라이브러러
| 졸리운_곰 | 2015.05.11 | 395 |
| 8 | Use Visual Studio C++ 6.0 on Windows 8.1 64bit : 윈도우 8.1에서 비주얼 스투디오 6.0 사용하기 | 졸리운_곰 | 2015.05.09 | 632 |
| 7 |
Visual Studio 2013을 사용하여 앱 개발
| 졸리운_곰 | 2014.05.30 | 842 |
| 6 |
C++로 작성한 Windows 스토어 앱용 로드맵
| 졸리운_곰 | 2014.05.30 | 570 |
| 5 |
블로그 뷰어 앱 만들기(C++)
| 졸리운_곰 | 2014.05.30 | 3956 |
| 4 |
C++ Windows 스토어 앱에서 파일 선택기 사용(자습서 4/4)
| 졸리운_곰 | 2014.05.30 | 762 |
| 3 |
C++ Windows 스토어 앱에서 탐색 및 보기 추가(자습서 3/4)
| 졸리운_곰 | 2014.05.30 | 594 |
| 2 |
C++ Windows 스토어 앱의 수명 주기 및 상태 관리(자습서 2/4)
| 졸리운_곰 | 2014.05.30 | 496 |
| 1 |
기본 C++ Windows 스토어 앱 만들기(자습서 1/4)
| 졸리운_곰 | 2014.05.30 | 857 |







