[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

 

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수

등록된 글이 없습니다.

대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED