■ ScrollViewer 엘리먼트에서 CanContentScroll 속성과 IScrollInfo 인터페이스를 사용해 컨텐츠를 스크롤하는 방법을 보여준다.
▶ 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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="600" Title="TestProject" FontFamily="나눔고딕코딩" FontSize="16"> <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"> <Border Width="500" Height="500" BorderThickness="2" BorderBrush="Black" Background="White"> <ScrollViewer Name="scrollViewer" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible" CanContentScroll="True"> <StackPanel Name="stackPanel"> <Button>Button 1</Button> <Button>Button 2</Button> <Button>Button 3</Button> <Button>Button 4</Button> <Button>Button 5</Button> <Rectangle Width="300" Height="300" Fill="Red" /> <TextBlock>Red Rectangle</TextBlock> <Rectangle Width="300" Height="300" Fill="Green" /> <TextBlock>Green Rectangle</TextBlock> <Rectangle Width="300" Height="300" Fill="Blue" /> <TextBlock>Blue Rectangle</TextBlock> </StackPanel> </ScrollViewer> </Border> <StackPanel HorizontalAlignment="Center" Margin="10" Orientation="Horizontal"> <Button Name="lineUpButton" Width="100" Height="30" Content="Line Up" /> <Button Name="lineDownButton" Margin="10 0 0 0" Width="100" Height="30" Content="Line Down" /> </StackPanel> </StackPanel> </Window> |
▶ MainWindow.xaml.cs
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
using System.Windows; using System.Windows.Controls.Primitives; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); this.lineUpButton.Click += lineUpButton_Click; this.lineDownButton.Click += lineDownButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region Line Up 버튼 클릭시 처리하기 - lineUpButton_Click(sender, e) /// <summary> /// Line Up 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void lineUpButton_Click(object sender, RoutedEventArgs e) { ((IScrollInfo)this.stackPanel).LineUp(); } #endregion #region Line Down 버튼 클릭시 처리하기 - lineDownButton_Click(sender, e) /// <summary> /// Line Down 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void lineDownButton_Click(object sender, RoutedEventArgs e) { ((IScrollInfo)this.stackPanel).LineDown(); } #endregion } } |