[C# WPF] WPF WrapPanel ScrollBar(스크롤바) 추가하는 방법

안녕하세요.

 

오늘은 WPF 에서 WrapPanel에 대해서 알아 보려고 합니다.

 

그 중에서도 WrapPanel 사용 시, 특정 컨트롤이 지정된 사이즈를 넘길 시에 자동으로 ScrollBar(스크롤바) 가 생기는 방법에 대해서 알려 드리려고 합니다.

 

이 부분은 매우 간단하기 때문에 별 다른 설명 없이 바로 예제 코드를 통해서 보여 드리도록 하겠습니다.

 

WPF 기본 프로젝트 하나를 생성해 주시고 xaml 코드를 아래와 같이 작성해 주시기 바랍니다.

 

 

MainWindow.xaml
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
<Window x:Class="WrapPanelTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WrapPanelTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="200" Width="400">
    <Grid>
        <ScrollViewer VerticalScrollBarVisibility="Auto">
            <WrapPanel>
                <Button Width="100" Height="100" Content="Button1"/>
                <Button Width="100" Height="100" Content="Button1"/>
                <Button Width="100" Height="100" Content="Button1"/>
                <Button Width="100" Height="100" Content="Button1"/>
                <Button Width="100" Height="100" Content="Button1"/>
                <Button Width="100" Height="100" Content="Button1"/>
                <Button Width="100" Height="100" Content="Button1"/>
                <Button Width="100" Height="100" Content="Button1"/>
                <Button Width="100" Height="100" Content="Button1"/>
            </WrapPanel>
        </ScrollViewer>
    </Grid>
</Window>
 
cs

 

실행 결과

 

위와 같이 WrapPanel 안에 여러 개의 Button 컨트롤을 배치했는데요. 기존 Window의 크기를 벗어나서 Button 컨트롤이 배치가 되면 위와 같이 자동으로 ScrollBar 컨트롤이 생성되는 것을 확인하실 수 있습니다.

 

감사합니다.^^

728x90

이 글을 공유하기

댓글

Designed by JB FACTORY